2014-11-08 57 views
0

当我在UiCollectionView中将addTarget添加到UIButton时出现以下错误。UIButton addTarget UIViewCollectionView中的参数

我的代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    UIButton *btnBonus = (UIButton *) [cell viewWithTag:405]; 
    [btnBonus setTag: [arrayTruebonusTags[0] intValue]]; 
    [btnBonus addTarget:self action:@selector(goBonus:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 

} 

- (void) goBonus:(id) sender 
{ 
    UIButton *button = (UIButton *) sender; 
} 

而且我得到这个错误:

[Controller goBonus]: unrecognized selector sent to instance 0x16dc1190 
2014-11-08 11:11:41.991 demo[3570:1707966] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Controller goBonus]: unrecognized selector sent to instance 0x16dc1190' 
*** First throw call stack: 
(0x299cdc1f 0x375b2c8b 0x299d3039 0x299d0f57 0x29902df8 0x2cebdc2b 0x2cebdbd1 0x2cea8863 0x2cebd63d 0x2ce8242d 0x2ceb72f1 0x2ceb6bcd 0x2ce8d3dd 0x2d100c29 0x2ce8be39 0x29994377 0x29993787 0x29991ded 0x298e0211 0x298e0023 0x30cbf0a9 0x2ceec1d1 0xf3599 0x37b32aaf) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

的问题是,如果我做同样的无goBonus:和方法-goBonus {}它就像魅力。

回答

1

您发布的崩溃日志抱怨丢失的方法[Controller goBonus]

您发布的代码显示您添加动作goBonus:(带冒号,表示需要一个参数)。

崩溃与您的代码不符的事实告诉我,您在某处存在不匹配。您的addTarget方法中的选择器@selector(goBonus:)对于您发布的方法而言是正确的,但是崩溃日志抱怨缺少选择器@selector(goBonus)(没有冒号,因此没有参数。)

您需要对此进行排序。

+0

你说得对!是我的错,在故事板中,我有一个以前的goBonus,我忘了删除。谢谢!。 – 2014-11-08 14:01:52

+0

如果我解决了您的问题,您应该接受我的回答,以便其他人知道问题已得到解答。 – 2014-11-08 14:53:22

+0

对不起,还有3分钟的时间来验证,但我还在做其他的事情,你在那里,谢谢! – 2014-11-08 16:09:37