Programming/iOS

presentModalViewController 호출 시 hierarchy 오류

알레아 2015. 10. 22. 10:25
반응형

UINavigationViewController내의 modalView가 최상위로 올려져 있는 상태에서

다시  modalView를 호출하려고 하면 Warning이 날 수 있습니다.

 

이는 modalView위에서 presentModalViewController를 호출해야하는데

UINavigationViewController에서 다시 호출하려고 하다 그런 문제가 생기기 때문입니다.

Warning: Attempt to present < TestModalViewController: 0x999999> on <MainViewController: 0x223r26> whose view is not in the window hierarchy!

 

위의 에러가 났을 경우에는

아마 대부분 이렇게 호출을 했을 것입니다.

[self presentModalViewController:test2ViewController animated:YES];

 

 

해결방법

아래와같이

현재 최상위 화면이 NavigationViewController인지 그 안의 modalView인지에 따라서

호출 하려고 하는 대상을 변경해주면 됩니다.

UIViewController *activeController = [UIApplication sharedApplication].keyWindow.rootViewController;

if ([activeController isKindOfClass:[UINavigationController class]])
    activeController = [(UINavigationController*) activeController visibleViewController];
else if (activeController.presentedViewController)
    activeController = activeController.presentedViewController;
                                                 
[activeController presentModalViewController:test2ViewController animated:YES];

 

 

 

출처

http://stackoverflow.com/questions/13350938/attempt-to-present-on-whose-view-is-not-in-the-window-hierarchy

반응형