ios - 如何从 Storyboard访问 tabBarViewController 中所有可能的 View Controller ?

标签 ios swift uitabbarcontroller

我想以编程方式构建 Storyboard tabBarController 的副本,如下所示:

   let storyboard = UIStoryboard(name: "Main", bundle: nil)
   let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarViewController")
   viewControllers = tabBarController.childViewControllers            

我的 Storyboard tabBarController 包含 7 个选项卡。但上面的代码只为我提供了 4 个第一(在 iPhone 上)。如何从 Storyboard 中获取其他选项卡?

最佳答案

问题是因为您使用了错误的属性来获取viewcontrollers。您应该使用 viewControllers而不是childViewControllers 。尝试下面的代码,它将返回 7 个 Controller 的 viewControllers

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "tabBarViewController") as! UITabBarController
let viewControllers = tabBarController.viewControllers;

关于ios - 如何从 Storyboard访问 tabBarViewController 中所有可能的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49593855/

相关文章:

swift - 在 Swift 中将 Bearer Token 添加到 header 时出现问题(HTTP POST 调用)

ios - 是否可以在 UITextview 链接选择上设置清除颜色?

iphone - 检测何时选择 UITabBarController 更多选项卡

iphone - 使用 stringWithFormat 时对 loadHTMLString 的解析

ios - 使用约束重新定义协议(protocol)功能,而无需公开方法

iphone - UIDocumentInteractionController 不适用于 iOS 6

xcode - 在 Xcode 中处理一组文件

swift - 以编程方式更改 View

iphone - iOS UIView 动画问题

iOS : instance variable in block