我的应用程序的主窗口包含一个基于 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/