button action not responding iphone

筅森魡賤 提交于 2019-12-13 05:55:06

问题


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;

回答1:


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

you are passing UIControlEventTouchUpOutside, should be UIControlEventTouchUpInside




回答2:


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:


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];


来源:https://stackoverflow.com/questions/11360548/button-action-not-responding-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!