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"]; //这里改
}