1

我想在按下 uibutton 时将它的标签传递给指定的方法,但是当我尝试这个时,我收到一条错误消息。请帮忙。代码:

   UIButton *deleteButton  = [[UIButton alloc]initWithFrame:CGRectMake(230, dateLabel.frame.origin.y, 70, 27)];
    [deleteButton addTarget:self action:@selector(deleteButtonPressed:i) forControlEvents:UIControlEventTouchDown];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.jpg"] forState:UIControlStateNormal];
    [deleteButton setTag:i];

进而

    -(void)deleteButtonPressed:(int)tag
{
    NSLog(@"Button Pressed");
    NSLog(@"%i", tag);


}
4

2 回答 2

1

事件处理程序本身不传递标签,它们传递整个按钮:

UIButton *deleteButton  = [[UIButton alloc]initWithFrame:CGRectMake(230, dateLabel.frame.origin.y, 70, 27)];
[deleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchDown];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.jpg"] forState:UIControlStateNormal];
[deleteButton setTag:i];

现在您可以从传入的按钮中获取标签,如下所示:

-(void)deleteButtonPressed:(UIButton*)button {
    NSLog(@"Button Pressed, tag=%i", button.tag);
}
于 2013-08-14T01:00:12.907 回答
0

您不能在 @selector 中传递参数。那应该只是@selector(deleteButtonPressed:)。

然后在action方法中:

    -(void)deleteButtonPressed:(UIButton *) sender
{
    NSLog(@"Button Pressed");
    NSLog(@"%d", sender.tag);


}
于 2013-08-14T01:00:23.007 回答