Access Container View Controller from Parent iOS

后端 未结 11 2209
遥遥无期
遥遥无期 2020-11-28 18:13

in iOS6 I noticed the new Container View but am not quite sure how to access it\'s controller from the containing view.

Scenario:

11条回答
  •  眼角桃花
    2020-11-28 18:22

    With generic you can do some sweet things. Here is an extension to Array:

    extension Array {
        func firstMatchingType() -> Type? {
            return first(where: { $0 is Type }) as? Type
        }
    }
    

    You can then do this in your viewController:

    var viewControllerInContainer: YourViewControllerClass? {
        return childViewControllers.firstMatchingType()!
    }
    

提交回复
热议问题