UIButton Click Event Does Not Trigger

I made a simple application in which I load different .xib for each orientation,

I have two xib Portxib and Landxib , in both xibs, I dragged a button and assigned

tag value for it as 1 in both xib.

and I wrote this code in my viewDidLoad

 UIButton *btn=(UIButton *)[self.view viewWithTag:1]; [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; 

here the selector method is used,

 -(void)BtnClick:(id)sender { NSLog(@"BtnClick"); } 

UIButton Click event does not fire ...

OR

Is there any other way to call the same button method as from orientation?

EDIT:

.h file

.m file

+4
source share
3 answers

Make sure that...

  • The button is in the root view in xib since you find it in self.view OR make sure it is placed in the correct view and uses this view with viewWithTag
  • You have assigned tag 1 , so just make sure tag 1 is in the same form, another wise button will not be recognized by the tag. If this is your case, just change the button tag to a very unusual number for this type, for example 999, or check something else.

EDIT

It seems to me that you will not recognize in viewDidLoad where you configured your code. So just comment out this code in viewDidLoad and change the changeOrientation method after loading nib.

 -(void)changeOrientation { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if(orientation==UIInterfaceOrientationPortrait || orientation==UIInterfaceOrientationPortraitUpsideDown) { [[NSBundle mainBundle] loadNibNamed:@"PortraitXib" owner:self options:nil]; } else { [[NSBundle mainBundle] loadNibNamed:@"LandscapeXib" owner:self options:nil]; } //Adding this code here makes sense that nib is loaded and after that we are recognizing button from loaded view of nib. UIButton *btn = (UIButton*)[self.view viewWithTag:1]; [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; } 

Hope this helps.

+6
source

In the header file add

 IBOutlet UIButton *btn; 

link to this btn from your xib files.

Here, in your case, the btn you are using does not reference xib files, so the method does not start

 -(IBAction)BtnClick:(id)sender { NSLog(@"BtnClick"); } 

make an IBAction function and reference it using nib btn. In this case, you do not need to explicitly add addtarget.

If you do not add a button in nib, do not forget to run init and addubview in the view

You can see this tutorial http://www.idev101.com/learn/interface_builder_connections.html

EDIT Check what prints?

  for (UIButton *but in [self.view subviews]) { NSLog("but.tag %d",but.tag); if(but.tag==1)// compare for same row elements { NSLog("Button Added"); [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; } } 
+5
source

You can add such a button. Add this didLoad method

  UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [aButton setTag:1]; [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [aView addSubview:aButton]; } // then ... - (void)buttonClicked:(UIButton*)button { NSLog(@"Button %d clicked.", [button tag]); } 
0
source

All Articles