当按钮是UICollectionViewCell的子视图时,未调用UIButton的目标方法

时间:2018-02-27 05:39:49

标签: ios objective-c uicollectionview uibutton

我在UICollectionViewCell中有一个UIButton。 UIButton有一个内部触摸事件的目标。如何确保在点击按钮而不是UICollectionView委托方法时调用此目标方法?

1 个答案:

答案 0 :(得分:0)

对于UICollection视图我正在使用此代码根据您的要求修改它

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *identifier = @"cellIdentifier";
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];


    for (UIView * subView  in cell.contentView.subviews)
    {
        if ([subView isKindOfClass:[UIButton class]])
        {
            yourButton = (UIButton *)[cell viewWithTag:103];
        }
    }

    [yourButton addTarget:self action:@selector(AddFriends:) forControlEvents:UIControlEventTouchUpInside];
    [yourButton setTintColor:[UIColor blackColor]];

    return cell;
 }


 -(void)AddFriends:(id)sender
 {
    [self showLoadingView];
    UIButton *btnTapped = (UIButton *)sender;
    NSString* detailsId = btnTapped.accessibilityIdentifier;
    NSLog(@"str ID : %@",detailsId);
    .
    .
    .
    .
 }