在Swift中遍历视图控制器层次结构 [英] Traverse view controller hierarchy in Swift

查看:289
本文介绍了在Swift中遍历视图控制器层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历Swift中的视图控制器层次结构并找到一个特定的类。这里是代码:

 扩展UIViewController {

func traverseAndFindClass< T:UIViewController>() - > UIViewController?{

var parentController = self.parentViewController as? T'
^
|
|
//错误:无法从类型'UIViewController?'找到用户定义的转换来键入'UIViewController'
while(parentController!= nil){

parentController = parentController !.parentViewController

}

return parentController

}

}

现在,我知道parentViewController属性返回一个可选的UIViewController,但我不知道如何以上帝的名义使Generic成为可选的类型。也许使用某种类型的where子句? c> UIViewController?,以便泛型类型
可以从上下文中推断出来。检查想要的类还有
在循环中完成,不仅在循环之前完成一次。



这应该是工作的:

 扩展UIViewController {

func traverseAndFindClass< T:UIViewController>() - > T' {

var currentVC = self
let parentVC = currentVC.parentViewController {
if let result = parentVC as? T {
返回结果
}
currentVC = parentVC
}
返回零
}
}

示例用法:

  if let vc = self.traverseAndFindClass()作为SpecialViewController? {
// ....
}






更新:上述方法无法按预期工作(至少不在调试
配置中)并且我已将问题
作为单独问题发布:可选绑定成功,如果它不应该。一个可能的解决方法(从该问题的答案)
似乎是取代

 如果让result = parentVC as ? T {... 

with

  if let result = parentVC as Any as? T {... 

或者删除方法定义中的类型约束:

  func traverseAndFindClass< T>() - > T' {






更新2:这个问题已经在Xcode 7中解决了,
traverseAndFindClass()方法现在可以正常工作。




Swift 4更新:

 扩展UIViewController { 

func traverseAndFindClass< T:UIViewController>() - > T' {
var currentVC = self
let parentVC = currentVC.parent {
if let result = parentVC as? T {
返回结果
}
currentVC = parentVC
}
返回零
}
}


I would like to traverse the view controller hierarchy in Swift and find a particular class. Here is the code:

extension UIViewController{

    func traverseAndFindClass<T : UIViewController>() -> UIViewController?{

        var parentController = self.parentViewController as? T?
                                    ^
                                    |
                                    |
        // Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController'
        while(parentController != nil){

            parentController = parentController!.parentViewController

        }

        return parentController

    }

}

Now, I know that the parentViewController property returns an optional UIViewController, but I do not know how in the name of God I can make the Generic an optional type. Maybe use a where clause of some kind ?

解决方案

Your method should return T? instead of UIViewController?, so that the generic type can be inferred from the context. Checking for the wanted class has also to be done inside the loop, not only once before the loop.

This should work:

extension UIViewController{

    func traverseAndFindClass<T : UIViewController>() -> T? {

        var currentVC = self
        while let parentVC = currentVC.parentViewController {
            if let result = parentVC as? T {
                return result
            }
            currentVC = parentVC
        }
        return nil
    }
}

Example usage:

if let vc = self.traverseAndFindClass() as SpecialViewController? {
    // ....
}


Update: The above method does not work as expected (at least not in the Debug configuration) and I have posted the problem as a separate question: Optional binding succeeds if it shouldn't. One possible workaround (from an answer to that question) seems to be to replace

if let result = parentVC as? T { ...

with

if let result = parentVC as Any as? T { ...

or to remove the type constraint in the method definition:

func traverseAndFindClass<T>() -> T? {


Update 2: The problem has been fixed with Xcode 7, the traverseAndFindClass() method now works correctly.


Swift 4 update:

extension UIViewController{

    func traverseAndFindClass<T : UIViewController>() -> T? {
        var currentVC = self
        while let parentVC = currentVC.parent {
            if let result = parentVC as? T {
                return result
            }
            currentVC = parentVC
        }
        return nil
    }
}

这篇关于在Swift中遍历视图控制器层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆