2011-03-02 2 views

답변

1

불행하게도, 당신은 사용자 정의보기로 만든 UIBarButtonItem들에 대한 작업을 실행할 수 없습니다. 이 작업을 수행하는 유일한 방법은 사용자 정의보기가 실제로 UIControl 또는 터치 이벤트에 응답하는 다른 항목 인 경우입니다.

3.2 이전 버전을 지원해야하는 경우 이미지보기 대신 버튼을 만들고 버튼에 작업을 설정하는 것이 가장 좋은 방법입니다. 당신이 3.2 이상을 지원 멀리 얻을 수 있다면, 당신은 단지보기에 UIGestureRecognizer을 추가 할 수 있습니다 (BTW : 코드에서, 이미지보기가 적절한 사용은 아래를 참조, 유출) :

// This works for iOS 3.2+ 
UIImageView imageView = [[UIImageView alloc] initWithImage:image]; 

// Add the gesture recognizer for the target/action directly to the image view 
// Note that the action should take a single argument, which is a reference to 
// the gesture recognizer. To be compatible with actions, you can just declare 
// this as (id)sender, especially if you don't use it. So the prototype for your 
// action method should be: -(void)myAction:(id)sender or -(void)myAction:(UIGestureRecognizer*)gestureRecognizer 
UITapGestureRecognizer tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)]; 
[imageView setUserInteractionEnabled:YES]; 
[imageView addGestureRecognizer:tapRecognizer]; 
[tapRecognizer release]; 

// Proceed with configuring the bar button item 
UIBarButtonItem button = [[UIBarButtonItem alloc] initWithCustomView:imageView]; 
[[self navigationItem] setRightBarButtonItem:button]; 
[button release]; 
[imageView release]; // you were leaking this 

이 지금은 작동합니다 당신이 원하지 않을지도 모르는 곳에서 UIButton을 기대하지 않고 예상대로 ...

+0

: UIButton (UIControl의 자손)을 사용하여 UIBarButtonItem의 customView를 사용한다면, 동작을 추가 할 수 있습니다 이 메서드를 사용하여 UIButton에 액션을 추가하여 UIBarButtonItem에 추가합니다 :'[theButton addTarget : self action : @selector (theAction :) forControlEvents : UIControlEventTouchUpInside]; – kevinlawler

관련 문제