Click event of UIButton is not getting fired
ios, ipad, iphone, uibutton, xib
Solution
Make sure...
- Button is placed in root view in `xib` as you are finding it in `self.view` OR make sure it is placed in correct view and use that view with `viewWithTag`
- You have assigned tag 1, so just make sure no other control within same view have tag 1 other wise button will not be recognized by tag. If this is going to be your case then just change tag of button to very unusual number for that view like 999 or any thing else and test.
EDIT
I am feeling like you view is not getting recognized at `viewDidLoad` where you have setup your code. So just comment out that code in `viewDidLoad` and shift in your 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.
Problem
I made a simple app in which i am loading different `.xib` on each orientation, I have two xib's `Portxib` and `Landxib`, in both xibs, i dragged a button and assigned a tag value to it as `1` in both xib's. and i wrote this code in my `viewDidLoad` ``` UIButton *btn=(UIButton *)[self.view viewWithTag:1]; [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside]; ``` here is the selector method, ``` -(void)BtnClick:(id)sender { NSLog(@"BtnClick"); } ``` Click event of UIButton is not getting fired... OR Is there any other way to call the same method of button from both the orientation ?? EDIT : `.h file` `.m file`