objective-c – 确定UIViewController是否以模态方式呈现

前端之家收集整理的这篇文章主要介绍了objective-c – 确定UIViewController是否以模态方式呈现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序的主窗口包含一个基于xib的UITabBarController(在Interface Builder中完全配置),也可以以模式显示(就像Music.app“添加歌曲到播放列表”模态视图). UITabBarController包含一些UINavigationController,它们又包含子类的UITableViewController.这是我当前正在检测的子类UITableViewController是否被呈现在一个模态的UITabBarController中:
- (void)viewDidLoad {
    [super viewDidLoad];

    self.isModal = NO;

    UIViewController *child     = self;
    UIViewController *parent    = self.parentViewController;
    while (parent) {
        if (parent.modalViewController && parent.modalViewController == child) {
            self.isModal = YES;
            break;
        }
        child   = parent;
        parent  = parent.parentViewController;
    }

    if (self.isModal) {
        // modal additions,eg. Done button,navigationItem.prompt
    }
    else {
        // normal additions,eg. Now Playing button
    }
}

有没有办法做到这一点,不涉及走上parentViewController树,或者将所有中间视图控制器子类化,以便在初始化时传递isModal状态?

解决方法

如果您正在寻找iOS 6,此答案已被弃用,您应该检查 Gabriele Petronella’s answer

之前我回答了一个非常类似的问题,在那里我有一个功能来确定当前控制器是否被呈现为模态,而不需要在此处对标签栏控制器进行子类化:

Is it possible to determine whether ViewController is presented as Modal?

在原来的答案中,有一些关于这个功能如何工作的基本解释,如果需要,你可以在那里检查,但是在这里

-(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,except by using a modal presentation
             [[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);

    }

    return isModal;        

}

猜你在找的C&C++相关文章