单击按钮时无法调用函数

时间:2011-06-02 10:16:50

标签: iphone ipad

我创建了一个UIBUtton

editButton = [[UIButton alloc]initWithFrame:CGRectMake(width, -4, 30, 30)];
        [editButton setImage:[UIImage imageNamed:@"DeleteButton.jpg"] forState:UIControlStateNormal];
        [editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
        editButton.hidden = YES;
        [editButton setTag:tag];
        [backView addSubview:editButton];

但是控制没有跳到它的定义,我检查按钮是否在点击时给出了它的标记值。

-

(void)deleteObjectViewImage
{

    NSInteger index = editButton.tag;
    editButton.hidden = YES;
}

任何人都可以告诉我为什么它不起作用。

4 个答案:

答案 0 :(得分:1)

[editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];

期望函数deleteObjectViewImage:有一个参数。但是函数定义没有任何。

更新:

-(void)deleteObjectViewImage: (id) sender
    {
    UIButton *clickedButton = (UIButton *) sender;
        NSInteger index = clickedButton.tag;
        editButton.hidden = YES;
    }

并使用

[editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];

答案 1 :(得分:1)

在代码中使用此行而不是行(在函数名后删除冒号),

 [editButton addTarget:self action:@selector(deleteObjectViewImage) forControlEvents:UIControlEventTouchUpInside];

答案 2 :(得分:0)

要获取Tag Value,您必须在Button Click事件中编写此代码...

 -(IBAction)deleteObjectViewImage:(id)sender
 {
      UIButton *btn = (UIButton *)sender;
      NSLog(@"%d",btn.tag);
 }

不要从

中删除冒号
 [editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];

答案 3 :(得分:0)

试试这个

editButton = [[UIButton alloc]initWithFrame:CGRectMake(width, -4, 30, 30)];
[editButton setImage:[UIImage imageNamed:@"DeleteButton.jpg"] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
editButton.hidden = YES;
[editButton setTag:tag];
[backView addSubview:editButton];


- (void)deleteObjectViewImage:(id)sender
{

    UIButton *button = (UIButton *)sender;
    NSLog(@"Button tag is %d",button.tag);
    button.hidden = YES;
}