2013-10-26 2 views
0

나는 barButtonItem 핸들러 메쏘드에 있으며 네비게이션 컨트롤러에 대한 참조가 필요합니다. 하지만 에는 저장된 참조 ~ 네비게이션 컨트롤러이 없습니다. 어떤 생각?UIBarButtonItem에서 포함 navigationController에 대한 참조를 얻으려면 어떻게해야합니까?

#import "ReportViewController.h" 
#import "CenterViewAnimationUtility.h" 

@implementation ReportViewController 

- (void)viewDidLoad 
{ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button addTarget:[CenterViewAnimationUtility class] action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 

//next file .. 

#import "CenterViewAnimationUtility.h" 

@implementation CenterViewAnimationUtility 

+ (void)buttonPressed:(UIBarButtonItem *)barButtonItem { 

    UINavigationController *navigationController = barButtonItem.//.. 
} 

답변

2

이 방법은 탐색 컨트롤러에있는 뷰 컨트롤러에있는 가정하면 간단하게 수행 할 수 있습니다

UINavigationController *navigationController = self.navigationController; 

질문 - 왜 단추 처리기 클래스의 방법이다? 이 솔루션은 버튼 핸들러를 적절한 인스턴스 메소드로 만드는 경우에만 작동합니다.

당신은 정말 클래스의 방법으로 대상을해야하는 경우 버튼의 target 호텔에서 뷰 컨트롤러에 대한 참조를 얻을 수 있습니다 :

UIViewController *target = barButtonItem.target; 
UINavigationController *navigationController = target.navigationController; 

업데이트 :

사실에 기초에 그 이 경우 단추의 대상이보기 컨트롤러가 아니기 때문에이 솔루션은 작동하지 않습니다.

+0

내가 다른 컨트롤러에 배치되어 더 UIBarButtonItems 및 NSButtons를 처리하는 유틸리티 클래스가 있습니다. Unfortunatelly 컨트롤러에는 UIViewController 및 UITableViewController와 같은 다른 유형이 있으므로 공통 클래스에 논리를 배치 할 수 있으므로 논리를 유틸리티 클래스에 넣을 수 있지만 어떻게 든 제어를 소유 한 컨트롤러에 대한 참조가 필요합니다. –

+0

@ János 내 대답의 두 번째 절반을 참조하십시오. – rmaddy

+0

두 번째 솔루션의 문제점은 유틸리티 클래스를 되돌려 주지만 컨트롤러와 아무 관련이 없습니다. –

0

다른 뷰 컨트롤러의 다양한 버튼에 사용 되었기 때문에 클래스 메소드로 만들고 있다는 것을 올바르게 이해하고 있습니까?

그렇다면 각 단추가 자신의 UIViewController 내의 메서드에 대상/동작 메시지를 보내고 해당 메서드가 매개 변수로 UINavigationController를 전달하여 공통 클래스 메서드를 호출하게하는 것이 좋습니다.

UINavigationController *navController = self.navigationController; 
[<classObject> buttonPressed:(UIBarButtonItem *)barButtonItem 
    fromNavigationcontroller:(UINavigationController *)navController; 

당신은 분명히 다시 작성하는 클래스 메소드 추가 매개 변수를 받아 들일 필요가있다 :

+ (void)buttonPressed:(UIBarButtonItem *)barButtonItem 
     fromNavigationcontroller:(UINavigationController *)navController{ 
관련 문제