2011-09-19 4 views
0

그래서 여기에 문제가 있습니다. 내가 UINavigationController가 스택에 푸시 된 UIViewController에의 부모 컨트롤러를 검색하기 위해이 코드를 사용하고 있습니다 :여러 개의 UINavigationController가있는 UITabBarController의 UINavigationController 스택에 뷰 컨트롤러 가져 오기

MyAppDelegate *delegate = [[UIApplicationDelegate sharedApplication] delegate]; 
UINavigationController *nav = delegate.navigationController; 
MyParentViewController *parent = (MyParentViewController *)nav.topViewController; 
parent.myProperty = @"propertyValue"; 

그러나, 이것은 당신이 하나의 탐색 컨트롤러를 사용하여 응용 프로그램으로 작업하는 경우에만 작동하는 것 같다. 내 구조는 :

->UITabBarController 
-->UINavigationController 
--->MyYetAnotherParentViewController 
-->UINavigationController 
--->MyOtherParentViewController 
-->UINavigationController 
--->MyParentViewController 

즉, 3 개의 내비게이션 컨트롤러가 탭 바 컨트롤러 안에 있음을 의미합니다.

현재 세 번째 내비게이션 컨트롤러에 있으며, MyParentViewController 위에 뷰 컨트롤러를 밀어 넣었습니다.

UIViewController에서 데이터를 전달하려고하는데 속성을 사용하여 MyParentViewController에 푸시했습니다. 이 설정을 사용하면 스택에 푸시 한 UIViewController의 부모를 어떻게 검색합니까?

답변

2

당신이 찾고있는 것이 맞는지 확실하지 않지만 [nav viewControllers] 배열의 맨 위에있는 ViewController라고 생각합니다.

2

이 방법으로이 작업을 수행 할 수 있더라도 MVC 디자인 패턴과 잘 맞지 않습니다. 이상적으로, 자녀 VC는 모델 클래스 (싱글 톤일 수 있음)의 메서드를 호출해야합니다. & 모델 클래스는 부모 VC에 알려야합니다. 당신이 뭔가를 할 수 같은 -

//In your child VC 
[[MyModelClass sharedInstance] buttonClickedInChildVC:@"propertyValue"]; 

//In your Model class 
[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonClickedInChildVC" object:@"propertyValue"]; 

//In your Parent VC 
[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(buttonClickedInChildVC:) 
               name:@"buttonClickedInChildVC" 
               object:nil]; 

-(void)buttonClickedInChildVC:(NSNotification *) notification 
{ 
    //do something 
} 

HTH,

하기 Akshay

관련 문제