-[UIView buttonClick:]: unrecognized selector sent to instance 0x8462da0

在view中我使用AFJSONRequestOperation下载数据,然后在success语法块中创建了一个按钮,在我点击这个按钮的时候,程序就crash了。

crash输出如下。

2013-11-01 11:35:47.898 JinglunPlatform[2914:c07] -[UIView buttonClick:]: unrecognized selector sent to instance 0x8462da0
2013-11-01 11:35:47.899 JinglunPlatform[2914:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView buttonClick:]: unrecognized selector sent to instance 0x8462da0'
*** First throw call stack:
(0x2531012 0x21dfe7e 0x25bc4bd 0x2520bbc 0x252094e 0x21f3705 0x11272c0 0x1127258 0x11e8021 0x11e857f 0x11e76e8 0x13eb1d3 0x24f9afe 0x24f9a3d 0x24d77c2 0x24d6f44 0x24d6e1b 0x2f077e3 0x2f07668 0x1123ffc 0x6172 0x24a5)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

我创建按钮的代码如下。

    AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 50, 20)];
                [button setImage:[UIImage imageNamed:@"btn_arrow_normal.png"] forState:UIControlStateNormal];
                [button addTarget:self.superview action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:button];
                [button release];
    }

网上一番搜索之后发现,原来是因为,addTarget后面跟的第一个参数self.superview变成了局部变量,然后在点击的时候就找不到这个已经释放掉的局部变量了。

解决的方法是,在类中定义一个成员变量,然后将这个参数换成这个成员变量。代码如下。

@property (strong, nonatomic) UIView * clickTarget;
//然后在类的初始化方法中。
self.clickTarget = self.superView;
//然后在使用的时候将self.superView替换为self.clickTarget。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值