unrecognized selector sent to instance error when a button is clicked

孤者浪人 提交于 2019-12-10 21:26:43

问题


I'm not entirely sure why I am getting this error. It occurs when I click the button on my FirstViewController:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstViewController 1ButtonPress:]: unrecognized selector sent to instance 0x10954bbd0'

My FirstViewController has a button (1ButtonPress) which performs a segue with another ViewController which has a embed NavigationController (so in my storyboard, the segue is from FirstViewController to the NavigationController). On this segue, some data is transferred to the other ViewController by:
First.m

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"abc"]) {
            UINavigationController *navController = [segue destinationViewController];
            SecondViewController *BVC = (SecondViewController *)(navController.topViewController);
        BVC.BLLImg = [UIImage imageNamed:@"SomeImage.jpg"];
        BVC.BLLTitle = @"SomeText";
    }

}

The segue has the identifier "abc" in storyboard. Looking around it, I believe it has something to do with an incorrect class, but everything on first inspection looks fine.
Any other reason why I would be getting this error?


回答1:


Let's go through the error you saw. It says that an unrecognized selector was sent to an instance of FirstViewController.

From what I deduce, you hooked up your button to your view controller, and removed the implementation from your view controller later, assuming that it'd remove the connection you earlier created from your button to your view controller. Unfortunately, that's not how it works. You need to remove the connection from the button too.

To do that, right click on your button in the storyboard, and remove the connection which says 1ButtonPress:.

Side note: You're doing too much for a beginner to understand. Please try going through the Cocoa world step by step. Also, read up this article to learn about how to name your methods and variables in a more easy-to-understand way.




回答2:


Right-click the 1ButtonPress button in your Storyboard. Remove the action to 1ButtonPress:.



来源:https://stackoverflow.com/questions/24588690/unrecognized-selector-sent-to-instance-error-when-a-button-is-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!