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

标签 objective-c ios

我的应用程序的主窗口包含一个基于 xib 的 UITabBarController(在 Interface Builder 中完全配置),它也可以模态显示(很像 Music.app 的“将歌曲添加到播放列表”模态视图)。 UITabBarController 包含许多 UINavigationController,而这些 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 树上遍历或子类化所有中间 View Controller 以在初始化时传递 isModal 状态?

最佳答案

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


我刚才回答了一个非常相似的问题,我有一个函数来确定当前 Controller 是否以模态形式呈现,而不需要在这里子类化标签栏 Controller :

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, then there is no way to do that, except by using a modal presentation
             [[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);

    }

    return isModal;        

}

关于objective-c - 确定 UIViewController 是否以模态方式呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4355951/

相关文章:

iphone - UITableView 滚动时崩溃并且 [reloadData]

ios - 如何在删除最后一项后隐藏 tableView 中的部分?

iOS - 将特定图像从包资源路径复制到文档目录

objective-c - 如何继承 UIScrollView 并将委托(delegate)属性设为私有(private)

objective-c - 核心数据整数改变值?

ios - 使用 FaSTLane/CircleCI 自动化 2FA

ios - Fabric.with Twitter 无故崩溃

ios - 在 Interface Builder 中创建的 UIView 未按预期呈现

ios - SSL 证书适用于 iOS VM,但不适用于设备

ios - 基于 UITableView Edit 重新排序 NSArray