您的位置:首页 > 移动开发 > IOS开发

【ios 异常FAQ】unrecognized selector sent to instance

2014-02-27 14:21 639 查看
unrecognized selector sent to instance 异常,一般是因为发送到消息对象被释放或者方法参数不匹配导致,因为@selector(XXX)在编译的时候没有明确现在参数类型;
一般在xcode中如何调试的时候直接定位到代码行,先设置 NSZombieEnabled为true,重新编译启动会直接定位到行。

eg:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show" style:0 target:self action:@selector(toggleAuthCodeBox:)];

- (void)toggleAuthCodeBox:(UIButton *)button   //这里应该为UIBarButtonItem,被强转,然后因为它没有setTitle:forState:方法导致;
{
self.loginBox.authCodeBoxVisible = !self.loginBox.authCodeBoxVisible;
[button setTitle:self.loginBox.authCodeBoxVisible ? @"Hide" : @"Show" forState:UIControlStateNormal];
}
正确的这么改:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show" style:0 target:self action:@selector(toggleAuthCodeBox:)];

- (void)toggleAuthCodeBox:(UIBarButtonItem *)button   //这里改为UIBarButtonItem
{
self.loginBox.authCodeBoxVisible = !self.loginBox.authCodeBoxVisible;
[button setTitle:self.loginBox.authCodeBoxVisible ? @"Hide" : @"Show"];  //这里改

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐