【问题标题】:Passing argument in button action method without use of tag在不使用标签的情况下在按钮操作方法中传递参数
【发布时间】:2011-09-08 12:07:19
【问题描述】:

我正在动态创建按钮。和创建操作方法

  [newButton addTarget:self action:@selector(goToNew:)forControlEvents:UIControlEventTouchUpInside];

我想从 tableview 发送参数(indexpath.row),但不想使用标签。因为我需要所有按钮的标签都保持不变,我如何在按钮操作中传递参数?

实际上我在每个 tableview 单元格中添加按钮,我想要对所有这些按钮进行操作,但是如果我使用 tag = indexpath.row 并将其与操作一起使用,它可以工作,但会发生覆盖按钮的问题。因此我想要标签是不变的。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UIButton *btn;

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(goToNew:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 55;
    [cell.contentView addSubview:btn];

}
else {
    btn = (id)[cell.contentView viewWithTag:55];

}

return cell;

}

- (void) goToNew:(id)sender   

{
UIButton *b = (UIButton *)sender;<br> UITableViewCell cell = (UITableViewCell)[b superview];
int row = [msgTableView indexPathForCell:cell].row;<br> (@"row is :::%d",row);

}

【问题讨论】:

标签: iphone objective-c ios uitableview uibutton


【解决方案1】:

通过以下代码,您将获得indexPath.row,无需设置按钮标签。

- (IBAction)goToNew:(id)sender 
{
    UIButton *btn = (UIButton*) sender;
    UITableViewCell *cell = (UITableViewCell*) [btn superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    int row = indexPath.row;
}

【讨论】:

  • 但是我怎么知道它是哪个按钮...意味着我的按钮在哪个单元格中,我用 sender.tag 得到它,但现在我怎样才能得到那个整数值?
  • 使用 btn.tag 可以得到 taht 按钮的标签
  • 我想不带标签。我希望标签保持不变。
  • @PJR,在上面的代码中,使用NSIndexPath *indexPath = [tableView indexPathForCell:cell]; int row = indexPath.row;
  • 如果要将按钮添加到cell.contentView,则应将其检索为UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
【解决方案2】:

您可以使用不同的 forstate 将另一个参数作为按钮的设置标题传递。这里您可以将 indexpath 作为按钮的标题传递。就像

    btn.tag=10;
    [btn setTitle:[NSString stringWithFormat:@"%d",indexpath.row] forState:UIControlStateDisabled];
    [btn addTarget:self action:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

你会收到参数

-(void)btnClick:(id)sender
{
UIButton* b = (UIButton*) sender;   
NSLog(@"tag=%d",b.tag);
   //Below line will got indexpath in string formate..
NSLog(@"title =%@",[b titleForState:UIControlStateDisabled]);
}

【讨论】:

    【解决方案3】:
    -(void)goToNew:(id)sender
    
    {
        UIButton *btnTemp = (UIButton *)[yourTableView viewWithTag:sender];
        int iTag = btnTemp.tag;  
    
    }  
    

    【讨论】:

    • 为此,当我创建按钮时,我必须指定 mybtn.tag = indexpath.row,对吗?但我希望该标签是恒定的,所以我想在创建按钮时不指定标签
    • 按钮没有标题。只有图像。我该如何使用它?
    • 我认为你应该使用标签来区分按钮......你可以使用行的索引,这样你就可以知道点击了哪一行的按钮......
    • 实际上我在每个 tableview 单元格中添加按钮,我想要对所有这些按钮进行操作,但是如果我使用 tag = indexpath.row 并将其与操作一起使用,它可以工作,但会出现覆盖按钮的问题发生。因此我希望标签是不变的。
    猜你喜欢
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2019-10-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多