簡體   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