如何检查viewcontroller是否添加到堆栈中

Gau*_*pta 11 uiviewcontroller ios swift

我有两个视图控制器.我按下按钮使用下面的代码,从一个视图导航到另一个视图.

 *let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("NotificationController") as! NotificationController

self.navigationController!.pushViewController(secondViewController, animated: true)*
Run Code Online (Sandbox Code Playgroud)

对于背面,我使用条形按钮上的条形按钮单击后面的代码.

 self.navigationController?.popViewControllerAnimated(true)
Run Code Online (Sandbox Code Playgroud)

所以我的问题是如果我从一个视图连续到另一个视图然后它添加到堆栈中.我想只显示另一个视图,当它已经添加到堆栈中以停止添加它时.它只添加一次.

PGD*_*Dev 18

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

if let viewControllers = self.navigationController?.viewControllers
{
    if viewControllers.contains(where: {
        return $0 is YourViewController
    })
    {
        //Write your code here
    }
}
Run Code Online (Sandbox Code Playgroud)

要从导航堆栈中删除特定控制器,您需要更改导航堆栈.

例:

    if var viewControllers = self.navigationController?.viewControllers
    {
        for controller in viewControllers
        {
            if controller is UIViewController
            {
                viewControllers.removeElement(controller)
                self.navigationController?.viewControllers = viewControllers
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


vp2*_*698 14

对于swift 4你可以使用

 if let viewControllers = self.navigationController?.viewControllers {
       for vc in viewControllers {
            if vc.isKind(of: YourViewController.classForCoder()) {
                 print("It is in stack")
                 //Your Process
            }
       }
 }
Run Code Online (Sandbox Code Playgroud)


Wom*_*ble 6

详细阐述了PGDev对Swift 4.1的回答

如何UIViewControllerUINavigationController堆栈中删除特定的子类:

/// Given 'nc' is a valid UINavigationController instance,
/// removes all instances of MyViewController from the stack

nc.viewControllers = nc.viewControllers.filter { !($0 is MyViewController) }
Run Code Online (Sandbox Code Playgroud)

把它作为一个扩展:

extension UINavigationController
{
    /// Given the kind of a (UIViewController subclass),
    /// removes any matching instances from self's
    /// viewControllers array.

    func removeAnyViewControllers(ofKind kind: AnyClass)
    {
        self.viewControllers = self.viewControllers.filter { !$0.isKind(of: kind)}
    }

    /// Given the kind of a (UIViewController subclass),
    /// returns true if self's viewControllers array contains at
    /// least one matching instance.

    func containsViewController(ofKind kind: AnyClass) -> Bool
    {
        return self.viewControllers.contains(where: { $0.isKind(of: kind) })
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

guard let nc = self.navigationController else { return }

let exists = nc.containsViewController(ofKind: MyViewController.self)

nc.removeAnyViewControllers(ofKind: MyViewController.self)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果有人知道如何约束'kind'到UIViewController的子类,请大声说出来.


Siv*_*ina 5

这是检查它的代码。

if let viewControllers = navigationController?.viewControllers {
    for viewController in viewControllers {
        // some process
        if viewController.isKindOfClass(ViewControllerClassName) {
            println("yes it is")
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)