如何检查视图控制器是否添加到堆栈中 [英] How to check viewcontroller is added in stack or not

查看:18
本文介绍了如何检查视图控制器是否添加到堆栈中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器.我已通过按下按钮使用以下代码从一个视图导航到另一个视图.

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.

推荐答案

要检查导航堆栈是否包含特定类型的view controller,您可以使用:

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
            }
        }
    }

这篇关于如何检查视图控制器是否添加到堆栈中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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