2012-12-27 69 views
2

我想TDD在我的應用程序中的一切,有沒有辦法看看是否有一個ViewController彈出模態明智?如何看到什麼viewcontroller呈現modal

如,如果在一個邏輯分支我打電話:

[self presentModalViewController:myModalControl]; 

有沒有辦法來測試這個,就會向呈現視圖 - 控制?

我想:

[mainVC_SUT presentedViewController] 

[mainVcSUT modalViewController] 

但都回來爲爲零。 mainVC_SUT是進行呈現的視圖控制器。

回答

0

檢查像這樣的:

if ([self.parentViewController.modalViewController isEqual:self]) 
    NSLog(@"I'm modal view controller!"); 
else 
    NSLog(@"I'm a push view controller!"); 
0
-(BOOL)isModal { 

    BOOL isModal = ((self.parentViewController && self.parentViewController.modalViewController == self) || 
        //or if I have a navigation controller, check if its parent modal view controller is self navigation controller 
        (self.navigationController && self.navigationController.parentViewController && self.navigationController.parentViewController.modalViewController == self.navigationController) || 
        //or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation 
        [[[self tabBarController] parentViewController] isKindOfClass:[UITabBarController class]]); 

    //iOS 5+ 
    if (!isModal && [self respondsToSelector:@selector(presentingViewController)]) { 

     isModal = ((self.presentingViewController && self.presentingViewController.modalViewController == self) || 
        //or if I have a navigation controller, check if its parent modal view controller is self navigation controller 
        (self.navigationController && self.navigationController.presentingViewController && self.navigationController.presentingViewController.modalViewController == self.navigationController) || 
        //or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation 
        [[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]); 

    } 

    return isModal;   

} 

信用:http://www.allenwei.cn/ios-determine-current-view-is-a-modal/

相關問題