UIButton addTarget,其中包含UIViewCollectionView中的参数

时间:2014-11-08 13:49:34

标签: ios objective-c uibutton uicollectionview

当我在UiCollectionView中将addTarget添加到UIButton时,我收到以下错误。

我的代码:

- (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 个答案:

答案 0 :(得分:1)

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

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

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

你需要对此进行排序。