mea*_*ers 109 objective-c uiviewcontroller uinavigationcontroller ios swift
在我的视图控制器代码中,我如何区分:
这两个presentingViewController
和isMovingToParentViewController
是YES
在这两种情况下,所以都不是很有益的.
令我感到困惑的是,我的父视图控制器有时是模态的,在其上推送要检查的视图控制器.
事实证明我的问题是我将我HtmlViewController
的UINavigationController
内容嵌入了一个然后呈现的内容.这就是为什么我自己的尝试和下面的好答案都不起作用的原因.
HtmlViewController* termsViewController = [[HtmlViewController alloc] initWithDictionary:dictionary];
UINavigationController* modalViewController;
modalViewController = [[UINavigationController alloc] initWithRootViewController:termsViewController];
modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:modalViewController
animated:YES
completion:nil];
Run Code Online (Sandbox Code Playgroud)
我想我最好告诉我的视图控制器何时是模态,而不是试图确定.
Col*_*gic 121
拿一粒盐,没试过.
- (BOOL)isModal {
if([self presentingViewController])
return YES;
if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])
return YES;
if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])
return YES;
return NO;
}
Run Code Online (Sandbox Code Playgroud)
Kin*_*ard 76
在Swift中:
// MARK: - UIViewController implementation
extension UIViewController {
var isModal: Bool {
let presentingIsModal = presentingViewController != nil
let presentingIsNavigation = navigationController?.presentingViewController?.presentedViewController == navigationController
let presentingIsTabBar = tabBarController?.presentingViewController is UITabBarController
return presentingIsModal || presentingIsNavigation || presentingIsTabBar
}
}
Run Code Online (Sandbox Code Playgroud)
rma*_*ddy 72
你忽略了一种方法:isBeingPresented
.
isBeingPresented
在呈现视图控制器时为true,在按下时为false.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([self isBeingPresented]) {
// being presented
} else if ([self isMovingToParentViewController]) {
// being pushed
} else {
// simply showing again because another VC was dismissed
}
}
Run Code Online (Sandbox Code Playgroud)
Jib*_*eex 27
self.navigationController!= nil意味着它在导航堆栈中.
为了处理当导航控制器以模态方式呈现时按下当前视图控制器的情况,我添加了一些代码行来检查当前视图控制器是否是导航堆栈中的根控制器.
extension UIViewController{
func isModal() -> Bool {
if let navigationController = self.navigationController{
if navigationController.viewControllers.first != self{
return false
}
}
if self.presentingViewController != nil {
return true
}
if self.navigationController?.presentingViewController?.presentedViewController == self.navigationController {
return true
}
if self.tabBarController?.presentingViewController is UITabBarController {
return true
}
return false
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*auz 25
Swift 3
这里是解决前面答案中提到的问题的解决方案,当推送isModal()
返回true
时UIViewController
,在给出的UINavigationController
堆栈中.
extension UIViewController {
var isModal: Bool {
if let index = navigationController?.viewControllers.firstIndex(of: self), index > 0 {
return false
} else if presentingViewController != nil {
return true
} else if navigationController?.presentingViewController?.presentedViewController == navigationController {
return true
} else if tabBarController?.presentingViewController is UITabBarController {
return true
} else {
return false
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止它对我有用.如果进行一些优化,请分享.
Cha*_*tas 12
斯威夫特4
var isModal: Bool {
return presentingViewController != nil ||
navigationController?.presentingViewController?.presentedViewController === navigationController ||
tabBarController?.presentingViewController is UITabBarController
}
Run Code Online (Sandbox Code Playgroud)
Kir*_*aev 10
Swift 5.干净简单。
if navigationController.presentingViewController != nil {
// Navigation controller is being presented modally
}
Run Code Online (Sandbox Code Playgroud)
Swift 5
这个方便的扩展比以前的答案处理更多的情况。这些情况是 VC(视图控制器)是应用程序窗口的根 VC,VC 作为子 VC 添加到父 VC。仅当视图控制器以模态方式呈现时,它才会尝试返回 true。
extension UIViewController {
/**
returns true only if the viewcontroller is presented.
*/
var isModal: Bool {
if let index = navigationController?.viewControllers.firstIndex(of: self), index > 0 {
return false
} else if presentingViewController != nil {
if let parent = parent, !(parent is UINavigationController || parent is UITabBarController) {
return false
}
return true
} else if let navController = navigationController, navController.presentingViewController?.presentedViewController == navController {
return true
} else if tabBarController?.presentingViewController is UITabBarController {
return true
}
return false
}
}
Run Code Online (Sandbox Code Playgroud)
感谢乔纳兹的回答。同样,还有更多优化的空间。需要处理的案例请在评论区讨论。
归档时间: |
|
查看次数: |
90544 次 |
最近记录: |