0

当我按下下一个按钮时,它是 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;
4

3 回答 3

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

你正在路过UIControlEventTouchUpOutside,应该是UIControlEventTouchUpInside

于 2012-07-06T10:35:38.393 回答
0

您将 forcontrolevent 作为 UIControlEventTouchUpoutside 传递,而不是使用

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

[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside];
于 2012-07-06T10:39:21.437 回答
0

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

但是,与其制作一个单独的按钮,然后将其作为视图分配给UIBarButtonItem,您可以简单地UIBarButtonItem使用所需的图像制作 a 并将所需的操作与它相关联。

例如:

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

self.navigationItem.rightBarButtonItem = nextButton;

[nextButton release];
于 2012-07-06T11:19:49.803 回答