繁体   English   中英

确定是否以模态方式呈现UIViewController

[英]Determining if a UIViewController is being presented modally

我的应用程序的主窗口包含一个基于xib的UITabBarController(在Interface Builder中完全配置),也可以模态呈现(很像Music.app“将歌曲添加到播放列表”模态视图)。 UITabBarController包含许多UINavigationControllers,后者又包含子类UITableViewControllers。 这就是我当前正在检测是否在模式UITabBarController中呈现子类UITableViewController:

- (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的答案


我刚才回答了一个非常类似的问题,我有一个函数来确定当前控制器是否显示为模态,而不需要在这里子类化标签栏控制器:

是否可以确定ViewController是否显示为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, then there is no way to do that, except by using a modal presentation
             [[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);

    }

    return isModal;        

}

由于iOS5的你也可以在一个的viewController实例中使用isBeingPresented:

- (BOOL)isModalViewController
{
    return [self isBeingPresented];
}

在Twitter上得到了答案 我最后UITabBarControllerUITabBarController并添加了一个BOOL isModal实例属性,当以模态方式呈现时,该属性设置为YES。 然后子视图可以使用self.tabBarController和子类的isModal来访问isModal属性并相应地渲染/表现。

我将看看获取根视图控制器并检查它是否有模态视图控制器。 您可以从UIWindow获取该视图控制器。 另请注意,您可以使用UINavigationController的viewControllers属性遍历当前视图控制器的层次结构:for(UIViewController * viewController in self.navigationController.viewControllers){...}更快更简单。

您可以在显示视图时在自定义初始化程序中设置显示状态。 我的意思是呈现它的代码将知道它是如何呈现的,对吧?

- (void)initInModalMode:(BOOL)isModal

它比后来追溯发现其状态更好吗?

在这些斯威夫特时代,有一种更容易的方式。

extension UIViewController {

    var isPresentedModally: Bool {
        return presentingViewController?.presentedViewController == self || parent?.isPresentedModally == true
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM