简体   繁体   中英

UIButton addTarget with parameters inside UIViewCollectionView

I'm getting the following error when I addTarget to UIButton inside UiCollectionView.

My code:

- (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;
}

And I get this error:

[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

The problem is that if I do the same without goBonus: and in the method -goBonus{} it works like a charm.

The crash log you posted complains about a missing method [Controller goBonus] .

The code you posted shows you adding the action goBonus: (with a colon, meaning it takes a parameter).

The fact that the crash doesn't match your code tells me that you have a mismatch somewhere. The selector in your addTarget method, @selector(goBonus:) , is correct for the method you posted, but the crash log is complaining about a missing selector @selector(goBonus) (no colon, hence no parameter.)

You need to sort that out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM