xcode开发中遇到unrecognized selector sent to instance 0x........的解决方法

 

 首先贴一下问题代码

 1 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 2     if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 3         UIButton *numberButton = [UIButton buttonWithType:UIButtonTypeCustom];        
 4         numberButton.frame = CGRectMake(10, 435, 46, 38);
 5         [numberButton setImage:[UIImage imageNamed:@"one.png"] forState:UIControlStateNormal];
 6         [numberButton addTarget:self action:@selector(numberButtonClick:) forControlEvents:UIControlEventTouchUpInside];
 7         [self.view addSubview: numberButton]; 
 8     }
 9 return self;
10 }
11 
12 -(IBAction)numberButtonClick:(id)sender{
13     NSLog(@"---");
14 }

整块代码看起来毫无问题,编译通过。然而一旦运行就会出现以下情形:

2010-03-16 22:23:58.811 Money[8056:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:'* -[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance 0x3d03ac0'

什么鬼?

reason:'* -[NSCFDictionary numberButtonClick:]: unrecognized selector sent to instance 0x3d03ac0'

NSCFDictionary调用了未知的方法:numberButtonClick:

造成这个错误的根本原因是在memory manage上,  持有 numberButtonClick:方法的对象A 被提前释放,指向A的指针成了野指针,在运行的时候指向了 NSCFDictionary  ,所以造成了程序的崩溃。

 

怎么解决呢?

我们可以将对象A 声明称 全局的变量 或者 属性  就可以保证 程序的正常运行了。

 





posted @ 2015-08-27 16:56  DarrenW  阅读(956)  评论(0编辑  收藏  举报