2017-03-06 60 views
1

我想比較的UIViewController在斯威夫特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) 
+0

工作,如果是VC arrViewControllers {}試試這個 –

+0

請解釋它是如何工作的?它是給出相同的錯誤... –

+0

嘗試'如果(vc是UIViewController)' –

回答

1

如果你想比較一個特定的視圖控制器,你必須比較他們的引用。

嘗試......

if(vc === viewController)) 
{ 
    return (self.navigationController?.popToViewController(vc, animated: animated)?.last)! 
} 
3
for viewsController in arrViewControllers 
{ 
    if(viewsController.isKind(of: YourControllerClassName.self)){ 
    } 
} 
+0

完全相同的錯誤'Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController + Extra.swift:18:30:無法將類型'UIViewController'的值轉換爲期望的參數類型'AnyClass'(又名'AnyObject.Type ')' –

+0

您是否使用過我提供的確切代碼? –

+0

你在哪裏得到錯誤? –

3

在迅速的,我們用is,而不是isKind(of:)

is用於檢查type of the object

所以,你可以使用,

if(vc is UIViewController) 

但我覺得在這裏你試圖match the 2 references of UIViewController

因此,您需要使用===而不是is。該運算符用於匹配相同類型的2個引用。

if(vc === viewController) 
+0

感謝您的解釋 –

0

你正在尋找可能是什麼,是,直到彈出一個給定的控制器,在給定的控制器是當前導航堆棧中的對象的方法。

所以,

這裏是彈出一個給定的控制器上面所有控制器的擴展方法。

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

我只是修改先生@BangOperator的移動到特定的視圖控制器的答案。

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

斯威夫特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)) 
     } 
    } 
    } 

}