在我的视图控制器代码中,我如何区分:
-
以模态呈现
-
推送到导航堆栈
在这两种情况下, presentingViewController
和_1449239都都是 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];
我想我最好告诉我的视图控制器何时是模态,而不是试图确定 .
14 回答
Swift 4
self.navigationController != nil
意味着它在导航堆栈中 .拿一粒盐,没试过 .
你忽略了一种方法:
isBeingPresented
.isBeingPresented
在呈现视图控制器时为true,在被推送时为false .在 Swift :
为了处理当导航控制器以模态方式呈现时按下当前视图控制器的情况,我添加了一些代码行来检查当前视图控制器是否是导航堆栈中的根控制器 .
Swift 3
这是解决前面答案中提到的问题的解决方案,当
isModal()
返回true
时,如果推送UIViewController
在呈现的UINavigationController
堆栈中 .到目前为止它对我有用 . 如果进行一些优化,请分享 .
正如这里的许多人建议的那样,“检查”方法并不适用于所有情况,在我的项目中,我提出了手动管理的解决方案 . 关键是,我们通常自己管理演示文稿 - 这不是幕后发生的事情,我们必须反省 .
DEViewController.h
文件:现在可以通过这种方式管理演示文稿:
pushed on navigation stack:
presented modally with navigation:
presented modally:
另外,在
DEViewController
中,如果上述属性等于SSViewControllerPresentationMethodUnspecified
,我们可以添加回退到"checking":假设您以模态方式呈现的所有viewControllers都包含在一个新的navigationController中(您应该始终这样做),您可以将此属性添加到VC中 .
要检测您的控制器是否被推或只是在您想要的任何地方使用下面的代码:
我希望这段代码可以帮助任何人......
如果您使用的是ios 5.0或更高版本,请使用此代码
}
这将告诉您是否呈现或推送了viewController
对于一些想知道的人,如何告诉ViewController它正在呈现
如果
A
正在展示/推动B
B
中定义enum
和property
A
视图控制器中,通过分配presentationStyle
告诉B
是否正在显示/推送它B
View Controller中的用法