将值从UIButton传递到UIActionSheet

时间:2010-04-19 18:26:10

标签: cocoa-touch uikit uibutton uiactionsheet

我正在尝试从按钮发送一个ActionSheet变量。

我不能使用tag属性,因为它被用于其他东西。

我已将myIndexRow声明为实例变量并具有:

NSInteger myIndexRow = indexPath.row;
    [deleteButton addTarget:self action:@selector(showDeleteSheet:) forControlEvents:UIControlEventTouchUpInside];

    deleteButton.myIndexRow = myIndexRow;

但我得到'请求会员'myRow'不是结构或联盟'

这里有一些显而易见的东西。

1 个答案:

答案 0 :(得分:1)

从来没有想过我会在SO上回答我自己的问题,但这里有:

访问superview的superview并查询indexPath部分和行属性就可以了。

在按钮的目标中:

-(void)showDeleteSheet:(id)sender {

NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[[sender superview] superview]];

    NSInteger currentSection = indexPath.section;
    NSInteger currentIndexRow = indexPath.row;

    //form the actionSheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Delete this item?" 
                                                         delegate:self 
                                                cancelButtonTitle:@"Cancel" 
                                           destructiveButtonTitle:@"Delete" 
                                                otherButtonTitles:nil];                         
    actionSheet.tag = currentIndexRow;
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.view];
    [actionSheet release];
}

然后在actionSheet的clickedButtonAtIndex方法中,我得到了我需要的行:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

     if (buttonIndex == 0)
        { ... doSomethingWith: actionSheet.tag }
     else
        { ... user pressed cancel }
}

部分答案是in another post and,其余here