按钮的动作没有响应iphone(button action not responding iphon

2019-10-29 04:20发布

当我按Next按钮,这是UIBarButton。 不进行再相关的操作。

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;

Answer 1:

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

你逝去的UIControlEventTouchUpOutside ,应该是UIControlEventTouchUpInside



Answer 2:

你通过了forcontrolevent作为UIControlEventTouchUpoutside而不是使用

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

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


Answer 3:

最明显的错误是UIControlEventTouchUpOutside ,你应该将其更改为UIControlEventTouchUpInside

但是,而不是做一个单独的按钮,然后作为一个着眼于分配给它UIBarButtonItem ,你可以简单地做一个UIBarButtonItem与所需的图像与所需的行动与之相关联。

例如:

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

self.navigationItem.rightBarButtonItem = nextButton;

[nextButton release];


文章来源: button action not responding iphone