5

I am trying to compare to UIViewController in Swift 3 but there is some error

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')

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'

I am calling it through this

self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), animated: false)
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84

5 Answers5

19
for viewsController in arrViewControllers
{
    if(viewsController.isKind(of: YourControllerClassName.self)){
    }
}
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
Punit
  • 1,330
  • 6
  • 13
  • exactly same error `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')` – Varun Naharia Mar 06 '17 at 12:02
9

Swift 4 Hope it will work for you

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

}
Saurav Kumar
  • 256
  • 3
  • 6
6

In swift, we use is instead of isKind(of:).

is is used to check the type 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.

So, you need to use === instead of is. This operator is used to match 2 references of same type.

if(vc === viewController)
PGDev
  • 23,751
  • 6
  • 34
  • 88
1

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)!
}
Adolfo
  • 1,862
  • 13
  • 19
0

i just modify the answer of Mr. @BangOperator for move to particular View controller.

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)
    }
  }
}
Mandeep Singh
  • 2,810
  • 1
  • 19
  • 31