Objective-c: action:@selector(xxxxxx)未调用。

时间:2022-01-16 22:54:50

I'm currently trying to add a button to my app. If the user push on it, it should display an alert.

我正在尝试给我的app添加一个按钮,如果用户按下它,它应该显示一个警告。

I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:

我想将这个按钮添加到tableview的section header中。这是我的tableView的代码:viewForHeaderInSection:

UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:@"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;

And in the same class, I have the method :

在同一个班级里,我有一个方法:

-(void)showMyAlert{
    NSLog(@"HERE SHOW ALERT");
    ...
}

The button is correctly displayed but the showMyAlert method is never called when I push on it. Does anyone have an idea of what is wrong with my code ? Thanks if you could help me :)

这个按钮是正确显示的,但是当我推它时,showMyAlert方法从来没有被调用。有人知道我的代码有什么问题吗?如果你能帮助我,谢谢。

4 个解决方案

#1


0  

Change your UIImageView as UIView and return it as the header view.

将UIImageView更改为UIView并将其作为头视图返回。

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];

#2


0  

Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.

确保img不是nil。如果这是nil,你的按钮将没有图像,你什么也看不到。

#3


0  

Modified below lines of code, Try like this:-

修改下面的代码行,试试这样:-。

[button addTarget:self action:@selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];

-(IBAction)showMyAlert:(id)sender{
    NSLog(@"HERE SHOW ALERT");
    ...
}

#4


0  

Try this, in viewDidLoad,

试试这个,viewDidLoad

-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:@"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}

#1


0  

Change your UIImageView as UIView and return it as the header view.

将UIImageView更改为UIView并将其作为头视图返回。

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];

#2


0  

Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.

确保img不是nil。如果这是nil,你的按钮将没有图像,你什么也看不到。

#3


0  

Modified below lines of code, Try like this:-

修改下面的代码行,试试这样:-。

[button addTarget:self action:@selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];

-(IBAction)showMyAlert:(id)sender{
    NSLog(@"HERE SHOW ALERT");
    ...
}

#4


0  

Try this, in viewDidLoad,

试试这个,viewDidLoad

-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:@"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:@selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}