如何检查viewcontroller是否添加到堆栈中 [英] How to check viewcontroller is added in stack or not
问题描述
我有两个视图控制器。我按下按钮使用下面的代码,从一个视图导航到另一个视图。
I have two view controllers. I have navigated from one view to another view by press the button to using below code.
*let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("NotificationController") as! NotificationController
self.navigationController!.pushViewController(secondViewController, animated: true)*
对于背面,我使用条形按钮上的条形按钮单击后面的代码。
For the back, I am using bar button on bar button click for back using below code.
self.navigationController?.popViewControllerAnimated(true)
所以我的问题是如果我从一个视图连续到另一个视图然后它添加到堆栈中。我想只显示另一个视图,当它已经添加到堆栈中以停止添加它时。它只会增加一次。
So my problem is if I am going from one view to another view continuously then it added in a stack. I want to only show another view when it is already added to the stack to stop adding it.It only adds one time.
推荐答案
要检查导航堆栈是否包含特定类型的视图控制器
,您可以使用:
To check whether the navigation stack contains a particular type of view controller
, you can use:
if let viewControllers = self.navigationController?.viewControllers
{
if viewControllers.contains(where: {
return $0 is YourViewController
})
{
//Write your code here
}
}
要从导航堆栈中删除特定控制器,您需要更改导航堆栈。
To remove a particular controller from navigation stack, you need to make changes to the navigation stack.
示例:
if var viewControllers = self.navigationController?.viewControllers
{
for controller in viewControllers
{
if controller is UIViewController
{
viewControllers.removeElement(controller)
self.navigationController?.viewControllers = viewControllers
}
}
}
这篇关于如何检查viewcontroller是否添加到堆栈中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!