How to fix: unrecognized selector sent to instance
I am having a problem that may be simple to fix, but not simple for me to debug. I simple have a button inside a view in IB; the file's owner is set to the view controller class I am using and when making the connections, everything seems fine, ie. the connector is finding the method I am trying to call etc.
however, I am receiving this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIApplication getStarted:]: unrecognized selector sent to instance 0x3d19130'
My code is as follows:
RootViewController.h
@interface RootViewController : UIViewController {
IBOutlet UIButton* getStartedButton;
}
@property (nonatomic, retain) UIButton* getStartedButton;
- (IBAction) getStarted: (id)sender;
@end
RootViewController.m
#import "RootViewController.h"
#import "SimpleDrillDownAppDelegate.h"
@implementation RootViewController
@synthesize getStartedButton;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction) getStarted: (id)sender {
NSLog(@"Button Tapped!");
//[self.view removeFromSuperview];
}
- (void)d开发者_高级运维ealloc {
[getStartedButton release];
[super dealloc];
}
@end
Seems simple enough...any thoughs?
It looks like you have released the RootViewController after add it to your window.
Post here the piece of code where you add RootViewController to your window.
BTW, try to comment the line where you do the release. So, instead of use:
RootViewController *viewController = [[RootViewController alloc] init];
[window addSubview:viewController.view];
[viewController release];
Do it:
RootViewController *viewController = [[RootViewController alloc] init];
[window addSubview:viewController.view];
//[viewController release];
Your method "getStarted" should work after that.
Cheers, VFN
You're sending getStarted
to UIApplication
and not RootViewController
. Double check to make sure the button is hooked up properly to the view controller in Interface Builder. Your code looks fine.
I have the same problem, and finally found the answer at this forum post: http://iphonedevbook.com/forum/viewtopic.php?f=25&t=706&start=0
When you instantiate your view controller class to be pushed into the navigation controller or similar, you have to make sure you instantiate it with your custom class name, and not UIViewController. This is easy to miss, and difficult to debug.
For example:
// this is wrong
RootViewController *viewController = [[UIViewController alloc] initWithNibName:@"TestView" bundle:nil];
// It should be
RootViewController *viewController = [[RootViewController alloc] initWithNibName:@"TestView" bundle:nil];
Hope it helps!
PS: Oops. Just noticed that your error says UIApplication, and not UIViewController, unlike mine. So probably like what Marc W said, somewhere you have used UIApplication where you should have used your custom class. It is probably something like my mistake.
精彩评论