I found some answers to my question but it still does not work. I have setup a button in IB and I want to setup the action and target by code(just for learning). I have a function in my MainViewController:
- (IBAction) onSpecialCase:(id)sender {
NSLog(@"It's working");
}
In the viewDidLoad function I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[myButton setTarget:self];
[myButton setAction:@selector(onSpecialCase:)];
}
Xcode already marks these lines telling me: “UIButton may not respond to ‘setTarget'”. When running the app anyways it terminates saying:
-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30
2011-06-29 08:12:22.465 FirstTry[3765:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton setTarget:]: unrecognized selector sent to instance 0x4b47e30'
That’s what I could expect from the XCode warning, but why is that? Every example I find on the web is exactly like mine. Adding the actionMethode to the button via IB works like expected. What am I doing wrong?
first check if you have declared the button as an outlet, second you should check if you connect the outlet to the button or use what I usually do is to add new button in code