0

我的 UITableViewCell 中的按钮出现问题。我正在使用故事板并通过 IB 连接我的按钮。在我的 cellforRowatIndexPath 中,我向按钮添加了一个操作:

cell.likeBtnPressed.tag = indexPath.row;
[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];

下面您将看到按下按钮时调用的内容:

-(void)userDidTapOnLikeButton:(UIButton *)button photo:(PFObject *)photo{
    //Disable the button so users cannot send duplicat requests
    [button setEnabled:NO];
    [button setTintColor:[UIColor redColor]];


    //Set the new state of the button
    BOOL liked = !button.selected;
    [button setEnabled:liked];

    //Get the current number of likes the post have
    NSString *originalButtonTitle = button.titleLabel.text;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];

    //Update the like count in the ECDCache
    NSNumber *likeCount = [numberFormatter numberFromString:button.titleLabel.text];
    if (liked) {
        likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
        [[ECDCache sharedCache] incrementLikerCountForPhoto:photo];
    }else{
        if ([likeCount intValue] > 0) {
            likeCount = [NSNumber numberWithInt:[likeCount intValue] - 1];
        }

        [[ECDCache sharedCache] decrementLikerCountForPhoto:photo];
    }

    //Add the current user as a liker of the photo in ECDCache
    [[ECDCache sharedCache] setPhotoIsLikedByCurrentUser:photo liked:liked];

    //Update the button label
    [button setTitle:[numberFormatter stringFromNumber:likeCount] forState:UIControlStateNormal];

    //Call the appropriate static method to handle creating/deleting the right object
    if (liked) {
        [ECDUtility likePhotoInBackground:photo block:^(BOOL succeeded, NSError *error) {
            [button setEnabled:YES];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0f)];
            [[button titleLabel] setShadowOffset:CGSizeMake(0.0f, -1.0f)];

            if (!succeeded) {
                // Revert the button title (the number) if the call fails
                [button setTitle:originalButtonTitle forState:UIControlStateNormal];
            }
        }];
    }
}

每当我按下按钮时,我都会收到:

-[UITouchesEvent objectId]: unrecognized selector sent to instance 0x15ee5de0

我不确定我做错了什么

4

2 回答 2

1

问题是您的方法接收的第二个参数不是 a PFObject,而是 a UIEvent

您可以发送三种类型的选择器addTarget:action:forControlEvents:

  • @selector(a): 这个方法没有参数。
  • @selector(a:):该方法有一个参数,UIControl即接收到控制事件的参数。
  • @selector(a:b:):这个方法有两个参数,一个UIControl是接收到控制事件的,一个UIEvent是触发它的。

由于您只想获取按钮,因此您应该有这样的签名:

-(void)userDidTapOnLikeButton:(UIButton *)button
{
    PFObject *photo = [self someLogicToGetThePhotoFromTheButton:button];
    ...
于 2013-10-02T16:46:56.997 回答
0

您不能在 UIButton 选择器中添加具有多个参数的目标。当调用选择器时,仅接收发件人参数,在这种情况下为uibutton对象。所以我建议你使用:

[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];

-(void)userDidTapOnLikeButton:(id)sender{
  //Your code
}

然后使用例如单元格的标签检索照片。

祝你好运 ;)

于 2013-10-02T16:32:50.987 回答