【ios 异常FAQ】unrecognized selector sent to instance

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值