是否可以确定 ViewController 是否显示为 Modal? [英] Is it possible to determine whether ViewController is presented as Modal?

查看:18
本文介绍了是否可以确定 ViewController 是否显示为 Modal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 ViewController 类内部检查它是否显示为模态视图控制器?

解决方案

由于 modalViewController 在 iOS 6 中已被弃用,这里有一个适用于 iOS 5+ 并且编译时没有警告的版本.

目标-C:

- (BOOL)isModal {返回 self.presentingViewController.presentedViewController == self||(self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController)||[self.tabBarController.presentingViewController isKindOfClass:[UITabBarController 类]];}

斯威夫特:

var isModal: Bool {返回 self.presentingViewController?.presentedViewController == self||(self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController)||self.tabBarController?.presentingViewController 是 UITabBarController}

Felipe 的回答提示.

Is it possible to check inside ViewController class that it is presented as modal view controller?

解决方案

Since modalViewController has been deprecated in iOS 6, here's a version that works for iOS 5+ and that compiles without warnings.

Objective-C:

- (BOOL)isModal {
    return self.presentingViewController.presentedViewController == self
      || (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController)
      || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
}

Swift:

var isModal: Bool {
    return self.presentingViewController?.presentedViewController == self
        || (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController)
        || self.tabBarController?.presentingViewController is UITabBarController
}

Hat tip to Felipe's answer.

这篇关于是否可以确定 ViewController 是否显示为 Modal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆