按钮动作没有响应iPhone

时间:2012-07-06 10:34:54

标签: iphone ios uibutton uibarbuttonitem

当我按下nextButton,这是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;

3 个答案:

答案 0 :(得分:5)

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

您正在通过UIControlEventTouchUpOutside,应该是UIControlEventTouchUpInside

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

明显的错误是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];