代码之家  ›  专栏  ›  技术社区  ›  Michael Morrison

将值从uibutton传递到uiactionsheet

  •  0
  • Michael Morrison  · 技术社区  · 16 年前

    我正在尝试通过按钮向操作表发送变量。

    我不能使用标记属性,因为它被用于其他用途。

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

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

    但我得到的是‘请求成员‘myrow’不是一个结构或联合体’。

    很明显我在这里找不到什么。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    从来没想过我会回答我自己的问题,但下面是:

    访问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