2012-07-06 109 views
0

当我按nextButton,这是UIBarButton。然后关联的动作不执行。按钮操作没有响应iphone

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; 

回答

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

你逝去的UIControlEventTouchUpOutside,应该是UIControlEventTouchUpInside

+0

哦,感谢它是由错误 – 2012-07-06 10:38:28

+0

@IdreesAshraf非常欢迎:)也许你可以接受的答案?谢谢。 – janusbalatbat 2012-07-06 10:40:05

0

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

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

[nextButton1 addTarget:self action:@selector(goToItems) forControlEvents:UIControlEventTouchUpInside]; 
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];