如何查看以模态方式呈现的viewcontroller

时间:2012-12-27 05:43:22

标签: iphone objective-c ios

我正在尝试在我的某个应用程序中对TDD进行TDD,有没有办法查看ViewController是否弹出模态?

如同,如果在逻辑分支中我打电话:

[self presentModalViewController:myModalControl];

有没有办法在正在呈现的viewcontroller上测试它?

我试过了:

[mainVC_SUT presentedViewController] 

[mainVcSUT modalViewController] 

但两者都回来了。 mainVC_SUT是执行演示的viewcontroller。

2 个答案:

答案 0 :(得分:0)

检查如:

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

答案 1 :(得分: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/