如何从navigationController的viewControllers中删除特殊控制器?

时间:2016-12-15 07:19:02

标签: ios swift uinavigationcontroller

我想用class编写一个params方法从viewController删除navigation controller,但我不知道如何处理它。

我的代码如下,我测试过,但没有成功:

class func removeVC(_ fromNav:UINavigationController, _ controller:UIViewController) {

    let controllers:NSArray = fromNav.viewControllers as NSArray
    for item in controllers {

        if (item as AnyObject).isMember(of:controller) { // There is not pass by Xcode

             // remove item out of fromNav.viewControllers
        }
    }
}

如何判断controller的{​​{1}} class等于controller中的参数swift

2 个答案:

答案 0 :(得分:0)

试试这个

方法1)

class func removeVC(fromNav: UINavigationController, controller: UIViewController) {

    let controllers = fromNav.viewControllers
    for item in controllers {

        if item == controller {
            fromNav.viewControllers.remove(at: fromNav.viewControllers.index(of: item)!)
        }

    }
}

用法:UtilSwift.navRemoveVC(self.navigationController!, self)

方法2):

class func navRemoveVC(_ fromNav:UINavigationController, withControllerClass:AnyClass) {

    let controllers = fromNav.viewControllers
    for item in controllers {
        if (item as AnyObject).isKind(of: withControllerClass.self) {
            fromNav.viewControllers.remove(at: fromNav.viewControllers.index(of: item)!)
        }
    }
}

用法:UtilSwift.navRemoveVC(self.navigationController!, withControllerClass:UserRegisterViewController.self)

答案 1 :(得分:0)

你可以试试这个:

   if (item as AnyObject).isKind(of: UIViewController.self){
        // remove item out of fromNav.viewControllers
   }