2012-07-06 154 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];