ios - 使用 nib ViewControllers 设置 UITabBarController

标签 ios swift xcode swift2 uitabbarcontroller

好的,坚持到这里,我们将不胜感激。

所以我在 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,这没有多大意义。

UIViewControllertabBarController 返回“ View Controller 层次结构中最近的祖先是标签栏 Controller ”。在您的情况下,这可能是 nil,因为您的标签栏 Controller 未包含在另一个标签栏 Controller 中。

您可能只想直接调用 self.setViewControllers

这种行为在 viewDidLoad 中可能比在 viewWillAppear 中更有意义,你真的想在每次标签栏出现时替换每个标签的 Controller 吗?

关于ios - 使用 nib ViewControllers 设置 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001814/

相关文章:

ios - Firebase:从嵌套的 Childs 中检索信息 (Swift 3)

xcode - 无法以XCode 6 Swift播放音频

ios - 如何在 Firestore 中删除 int、double、float 的可选值?

iPhone - 图像导致滚动和其他功能崩溃

ios - 源 View Controller 在自定义 segue 期间隐藏

ios - 在 UITabBarController 内的 View Controller 之间传递数据,无需使用 segues

iphone - 在 Storyboard 中编辑自定义 UIView 子类

xcode - Mac OS X 10.8.5 Xcode 5.0.2 配置错误 : clang: error: argument to '-V' is missing (expected 1 value) clang: error: no input files

ios - 首次启动时调试 iOS 应用程序

ios - 为什么 SimpleAuth CocoaPod 不能与 Swift Bridging Header 文件一起使用?