开发者

Passing values from UIButton to an UIActionSheet

I'm trying to send an ActionSheet a variable from a button.

I can't use the tag property because its being used for something else.

I've declared myIndexRow as an instance variable and have:

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

    del开发者_运维百科eteButton.myIndexRow = myIndexRow;

but I'm getting the 'Request for member 'myRow' is something not a structure or union'

There is something obvious I am missing here.


Never figured I'd answer my own question on SO but here goes:

Accessing the superview's superview and querying the indexPath section and row properties did the trick.

In the target of the button:

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

Then down in the actionSheet's clickedButtonAtIndex method I get the row I need:

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

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

Part of the answer was found in another post and and the rest here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜