好的,坚持到这里,我们将不胜感激。
所以我在 Storyboard 中的导航 Controller 内有一个选项卡 Controller 。
然后我有两个带有 nib 文件的 View Controller ,我想以编程方式将它们加载到选项卡 Controller 中。我正在使用以下代码,但在加载应用程序时出现空白屏幕。
class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)
let verbViewController = VerbViewController(nibName: "VerbViewController", bundle: nil)
let communityViewController = CommunityViewController(nibName: "CommunityViewController", bundle: nil)
let tabIcon1 = UITabBarItem(title: nil, image: UIImage(named: "VerbTab"), tag: 0)
let tabIcon5 = UITabBarItem(title: nil, image: UIImage(named: "CommunityTab"), selectedImage: nil)
verbViewController.tabBarItem = tabIcon1
communityViewController.tabBarItem = tabIcon5
let tabControllers = [verbViewController, communityViewController]
self.tabBarController?.setViewControllers(tabControllers, animated: true)
self.viewControllers = tabControllers
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Delegate method
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
return true
}
最佳答案
您正在从 UITabBarController
中调用 self.tabBarController
,这没有多大意义。
UIViewController
的 tabBarController
返回“ View Controller 层次结构中最近的祖先是标签栏 Controller ”。在您的情况下,这可能是 nil,因为您的标签栏 Controller 未包含在另一个标签栏 Controller 中。
您可能只想直接调用 self.setViewControllers
。
这种行为在 viewDidLoad
中可能比在 viewWillAppear
中更有意义,你真的想在每次标签栏出现时替换每个标签的 Controller 吗?
关于ios - 使用 nib ViewControllers 设置 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001814/