UIButtons not working
I have some Objective-C code to transition between two views. I have the transition code. What I would like to do is transition to a view on a button press, however this only seems to work when I generate the button with code, it won't work with a button I create in IB and link to the code (using File's Owner, etc.).
Here is my some of my code:
- (void)goToTwo {
ALPHAAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:2];
}
- (void)viewDidLoad {
NSLog(@"load01");
//Button Code
UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnOne.frame = CGRectMake(40, 40, 240, 30);
[btnOne setTitle:@"One!" forState:UIControlStateNormal];
[btnOne addTarget:self action:@selector(goToTwo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnOne];
[super viewDidLoad];
}
When the view loads, I am creating a button so that I have two, one created with code, one in IB, both of which 开发者_Go百科at the moment will do the same thing (transition to next view),
My .h file:
#import <UIKit/UIKit.h>
@interface View1 : UIViewController {
}
-(IBAction) goToTwo:(id)sender;
@end
I'm pretty sure I linked the button to the function correctly, dragging the node next to TouchUpInside over to the File's Owner and then clicking on the method name.
Any ideas on why the code generated button works and the IB button doesn't?
Thanks a lot in advance.
The IBAction is named goToTwo:
(colon at end) but the method implemented in the .m file and called by the button created in code is goToTwo
(no colon at end).
Either change the IBAction (and re-connect in IB) or change the method implementation in the .m file to match the declaration in the .h file.
精彩评论