ios - 带有 UINavigationController 作为选项卡的 UITabViewController

标签 ios uiviewcontroller swift3 uitabbarcontroller

我的数据由 UITabBarController 的两个选项卡共享。第一个选项卡负责生成和更新由第二个选项卡使用的数据数组。

我原来在选项卡之间共享数据的方法奏效了。

                      ------UIViewController
---UITabBarController
                      ------UIViewController

使用上述方法,我访问了第一个选项卡中的数据数组:

let pointsViewController = self.tabBarController?.viewControllers?.first as! ShowPointsViewController
tableHeaders = pointsViewController.tableHeaders 

将 ShowPointsViewController 作为第一个选项卡

这是我的问题: 当我将我的第一个 UIViewController 嵌入到 UINavigationController 中,然后尝试使用下面的代码访问我的数据数组时,我收到运行时错误,因为 之后的代码。首先... 返回为 nil。我知道我访问的方式不正确,获得相同结果的正确方法是什么?

 let pointsViewController = self.tabBarController?.viewControllers?.first?.navigationController?.topViewController as! ShowPointsViewController

最佳答案

如果我理解你创建了这样的东西:

                      ------UINavigationController    ------UIViewController
---UITabBarController
                      ------UINavigationController    ------UIViewController

你得到的是 nil,因为 self.tabBarController?.viewControllers?.first 已经是 UINavigationController。

使用以下代码:

 let pointsViewController = (self.tabBarController?.viewControllers?.first as? UINavigationController)?.topViewController as! ShowPointsViewController

关于ios - 带有 UINavigationController 作为选项卡的 UITabViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40935830/

相关文章:

ios - 从后台获取加载 UIWebView

iphone - 知道为什么这个 View 不旋转吗?

objective-c - ViewController 仅调用一次 TouchMoved

iphone - 在 UIViewController 内部集成 UIView 和 GLKView

swift3 - Swift 3.0 中 UIAlertAction 内的 UIAlertView ?

ios - 我对如何绘制空心圆角矩形感到很困惑。我怎样才能做到这一点?

ios - 错误域=com.alamofire.error.serialization.response 代码=-1011“请求失败 : forbidden (403)

ios - swift 3 : Transfer Utility enumerateToAssignBlocks method signature

mysql - xcode 中数组为空

ios - 从 UnitTest 启动 ViewDidAppear