【发布时间】:2017-05-10 12:41:29
【问题描述】:
如果您需要更改某些内容,我想知道您如何导航到 UIViewController 堆栈中更高级别的视图。
其中一个例子是如果您有 TabViewController 并且需要在选项卡之间进行通信,但这不是唯一的情况。
我现在做的是这样的
let tabOne = self.tabBarController?.childViewControllers.first?.childViewControllers.first as? tabOneController
或
let topController = ((self.viewControllers[0] as! RootViewController).viewControllers![0] as! UINavigationController).topViewController as? MySpecialViewController
这种方法的问题是你真的需要知道视图的层次结构,你必须依赖它,如果你稍微改变层次结构,你就需要改变它。
第二个问题是,如果您在一个团队中工作,那么这段代码非常令人沮丧,并且对于其他团队成员来说描述性不够。
我的问题是,什么是正确的方法/最佳实践,或者:您如何做到这一点?
【问题讨论】:
标签: swift xcode uiviewcontroller structure code-readability