简体   繁体   English

如何比较Swift 3中的UIViewController?

[英]How to compare UIViewController in Swift 3?

I am trying to compare to UIViewController in Swift 3 but there is some error 我想在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: Cannot convert value of type 'UIViewController' to expected argument type 'AnyClass' (aka 'AnyObject.Type') /Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30:无法将'UIViewController'类型的值转换为预期的参数类型'AnyClass'(又名'AnyObject.Type')

and if try to use 如果尝试使用

if(vc is viewController)

It gives 它给

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

I am calling it through this 我通过这个称呼它

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

Swift 4 Hope it will work for you 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))
      }
    }
  }

}

In swift, we use is instead of isKind(of:) . 在swift中,我们使用is而不是isKind(of:)

is is used to check the type of the object . is用于检查type of the objecttype of the object

So you can use, 所以你可以使用,

if(vc is UIViewController)

But I think here you are trying to match the 2 references of UIViewController . 但我想在这里你试图match the 2 references of UIViewController

So, you need to use === instead of is . 所以,你需要使用===而不是is This operator is used to match 2 references of same type. 此运算符用于匹配2个相同类型的引用。

if(vc === viewController)

If you want to compare to a particular view controller you have to compare their refererences. 如果要与特定视图控制器进行比较,则必须比较它们的参考。

Try this... 尝试这个...

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

Probably what you are looking for, is , a method that pops till a given controller, where the given controller is an object in current navigation stack. 可能你正在寻找的是一种方法,直到给定的控制器,其中给定的控制器是当前导航堆栈中的对象。

So, 所以,

Here is the extension method that pops up all controllers above a given controller. 这是一个扩展方法,弹出给定控制器上方的所有控制器。

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

i just modify the answer of Mr. @BangOperator for move to particular View controller. 我只是修改了@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