Iphone 在不使用标记的按钮操作方法中传递参数
我正在动态创建按钮。和创建操作方法Iphone 在不使用标记的按钮操作方法中传递参数,iphone,objective-c,ios,uitableview,uibutton,Iphone,Objective C,Ios,Uitableview,Uibutton,我正在动态创建按钮。和创建操作方法 [newButton addTarget:self action:@selector(goToNew:)forControlEvents:UIControlEventTouchUpInside]; 我想从tableview发送参数(indexpath.row),但不想使用标记。。因为我需要所有按钮的标记都保持不变,所以如何在按钮操作中传递参数 实际上,我正在每个tableview单元格中添加按钮,我希望所有这些按钮都有操作,但如果我使用tag=index
[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;
}
{UIButton*b=(UIButton*)发送方;
UITableViewCell=(UITableViewCell)[b superview];int行=[msgTableView indexPathForCell:cell]。行;
(@“行为::%d”,行)
通过执行以下代码,您将获得indexPath.row,无需设置按钮标记
- (IBAction)goToNew:(id)sender
{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) [btn superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
int row = indexPath.row;
}
您可以使用不同的forstate将另一个参数作为按钮的集合标题传递
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]);
}
我不明白“但不想使用标签…”看看我的答案:但是我怎么知道它是哪个按钮…意味着我的按钮在哪个单元格中,我用sender.tag得到它,但是现在我怎么能得到那个整数值呢?使用btn.tag你可以得到taht按钮的标记我想不带标记的做它。。我想要标记将保持不变。@PJR,在上面的代码中,使用
nsindepath*indepath=[tableView indexPathForCell:cell];int row=indexPath.row;
如果要将按钮添加到cell.contentView
,则应将其检索为,UITableViewCell*cell=(UITableViewCell*)btn.superview.superview;
为此,当我创建按钮时,我必须检查mybtn.tag=indexpath.row,对吗?但我希望该标记是常量,所以我希望在创建按钮时不指定标记。按钮没有标题。只有图像。我如何使用它?我认为您应该使用标记,以便能够区分按钮ns…ot你们可以使用行的索引,这样你们就可以知道在哪一行的按钮被点击了…实际上我在每个tableview单元格中添加了按钮,我希望所有这些按钮都有动作,但若我使用tag=indexpath.row并将其与动作一起使用,它会工作,但重叠按钮的问题会发生。所以我希望标记是常量。让我们来看看
-(void)goToNew:(id)sender
{
UIButton *btnTemp = (UIButton *)[yourTableView viewWithTag:sender];
int iTag = btnTemp.tag;
}
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]);
}