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

Sha*_*man 9 objective-c ios

我的应用程序的主窗口包含一个基于xib的UITabBarController(在Interface Builder中完全配置),也可以模态呈现(很像Music.app"将歌曲添加到播放列表"模态视图).该的UITabBarController包含许多又包含子类UITableViewControllers UINavigationControllers的.这就是我,如果子类的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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Fel*_*ino 10

如果您正在寻找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;        

}
Run Code Online (Sandbox Code Playgroud)


Nie*_*ole 5

由于iOS5的,你也可以使用isBeingPresented上的viewController实例:

- (BOOL)isModalViewController
{
    return [self isBeingPresented];
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*man 4

在 Twitter 上得到了答案。我最终进行了子类化UITabBarController并添加了一个 BOOLisModal实例属性,该属性在模态呈现时设置为 YES。然后子视图可以使用self.tabBarController对子类的强制转换来访问isModal属性并相应地呈现/行为。

  • 转换为子类。这看起来很尴尬。为什么要子类?为什么不向 UITabBarController 添加一个包含 isModal 的类别? (3认同)