button action not responding iphone

2019-09-02 11:39发布

When I press the nextButton, which is UIBarButton. Then associated action is not performed.

UIButton *nextButton1=[UIButton buttonWithType:UIButtonTypeCustom];
[nextButton1 setBackgroundImage:[UIImage imageNamed:@"next.png"] forState:UIControlStateNormal];
[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpOutside];
[nextButton1 sizeToFit];

UIBarButtonItem *nextButton=[[UIBarButtonItem alloc]initWithCustomView:nextButton1];
self.navigationItem.rightBarButtonItem=nextButton;

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-02 11:58

you passed the forcontrolevent as UIControlEventTouchUpoutside instead of that use

[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];

[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];
查看更多
迷人小祖宗
3楼-- · 2019-09-02 12:21
[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];

you are passing UIControlEventTouchUpOutside, should be UIControlEventTouchUpInside

查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-02 12:21

The obvious mistake is UIControlEventTouchUpOutside, you should change it to UIControlEventTouchUpInside.

But instead of making a separate button and then assigning it as a view to the UIBarButtonItem, you can simply make a UIBarButtonItem with the desired image and associate the desired action with it.

For example:

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next.png"]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self 
                                                              action:@selector(goToItems)];

self.navigationItem.rightBarButtonItem = nextButton;

[nextButton release];
查看更多
登录 后发表回答