2

我正在尝试对我的一个应用程序中的所有内容进行 TDD,有没有办法查看 ViewController 是否以模态方式弹出?

如,如果在逻辑分支中我调用:

[self presentModalViewController:myModalControl];

有没有办法在呈现的视图控制器上测试这个?

我试过了:

[mainVC_SUT presentedViewController] 

[mainVcSUT modalViewController] 

但两者都返回为零。mainVC_SUT 是执行呈现的视图控制器。

4

2 回答 2

0

像这样检查:

 if ([self.parentViewController.modalViewController isEqual:self])
    NSLog(@"I'm modal view controller!");
else 
    NSLog(@"I'm a push view controller!");
于 2012-12-27T06:56:05.050 回答
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/

于 2012-12-27T07:24:27.557 回答