繁体   English   中英

如何比较Swift 3中的UIViewController?

[英]How to compare UIViewController in Swift 3?

我想在Swift 3中与UIViewController进行比较,但是有一些错误

extension UINavigationController
{
    func myPopToViewController(viewController:UIViewController, animated:Bool) -> UIViewController? {
        var arrViewControllers:[UIViewController] = []

        arrViewControllers = self.viewControllers
        for vc:UIViewController in arrViewControllers {
            if(vc.isKind(of: viewController) ) // This Line gives me error
            {
                return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
            }

        }
        return nil
    }

}

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30:无法将'UIViewController'类型的值转换为预期的参数类型'AnyClass'(又名'AnyObject.Type')

如果尝试使用

if(vc is viewController)

它给

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:22:使用未声明的类型'viewController'

我通过这个称呼它

self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), animated: false)
for viewsController in arrViewControllers
{
    if(viewsController.isKind(of: YourControllerClassName.self)){
    }
}

Swift 4希望它对你有用

   extension UINavigationController {
  func myPopToViewController(viewController:UIViewController, animated:Bool) {
    var arrViewControllers:[UIViewController] = []
    arrViewControllers = self.viewControllers
    for vc:UIViewController in arrViewControllers {
      if(vc.isKind(of: viewController.classForCoder)){
         (self.popToViewController(vc, animated: animated))
      }
    }
  }

}

在swift中,我们使用is而不是isKind(of:)

is用于检查type of the objecttype of the object

所以你可以使用,

if(vc is UIViewController)

但我想在这里你试图match the 2 references of UIViewController

所以,你需要使用===而不是is 此运算符用于匹配2个相同类型的引用。

if(vc === viewController)

如果要与特定视图控制器进行比较,则必须比较它们的参考。

尝试这个...

if(vc === viewController) )
{
    return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
}

可能你正在寻找的是一种方法,直到给定的控制器,其中给定的控制器是当前导航堆栈中的对象。

所以,

这是一个扩展方法,弹出给定控制器上方的所有控制器。

extension UINavigationController {
    func popTo(controllerToPop:UIViewController)  {
        let controllers = self.viewControllers
        for controller in controllers {
            if(controller == controllerToPop) {
                self.popTo(controllerToPop: controllerToPop)
            }
        }
    }
}

我只是修改了@BangOperator先生的答案,转移到特定的View控制器。

extension UINavigationController {

func popTo(controllerToPop:UIViewController)  {

    //1. get all View Controllers from Navigation Controller
    let controllersArray = self.viewControllers

    //2. check whether that view controller is exist in the Navigation Controller
    let objContain: Bool = controllersArray.contains(where: { $0 == controllerToPop })

    //3. if true then move to that particular controller
    if objContain {
        self.popToViewController(controllerToPop, animated: true)
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM