iOS - Split View Controller - How do I get a pointer (reference) to the Detail View Controller from inside the Master View Controller?

前端 未结 2 488
梦如初夏
梦如初夏 2020-12-25 13:06

iOS - Split View Controller - How do I get a pointer (reference) to the Detail View Controller (the bigger right one) from inside the Master View Controller (the smaller lef

2条回答
  •  爱一瞬间的悲伤
    2020-12-25 13:33

    Create a property in your UISplitViewController subclass:

    var _detailViewController: UIViewController? {
        get {
            if viewControllers.count > 1 {
                return viewControllers[1] as? UIViewController
            }
            return nil
        }
    }
    

    According to Apple's documentation, this should sometimes return nil, but in my experience, it always returns the detail view controller, regardless of state.

    Also, do not call this property "detailViewController" instead of "_detailViewController" - Apple is apparently already using that name under the hood, and it will mess with your UI.

    UISplitViewController is really hokey and needs a lot of cleanup and corrected documentation...

提交回复
热议问题