【问题标题】:Add existing UIViewControllers without nib files to UITabBarController in swift快速将没有 nib 文件的现有 UIViewControllers 添加到 UITabBarController
【发布时间】:2015-02-01 19:21:04
【问题描述】:

我有两个 UIViewController 类,分别称为 MeasurementsVC.swift 和 OutfitBuilder.swift。但是,我没有这些 UIViewControllers 的 nib 文件。我想将它们加载到 UITabBarController 中,以便可以在两个 UIViewController 之间切换。基于the swift swift tutorial,我的 AppDelegate 文件中有以下内容

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.

    let tabBarController = UITabBarController()
    let myVC1: UIViewController = OutfitBuilderVC()
    let myVC2: UIViewController = MeasurementsVC()
    let controllers = [myVC1,myVC2]
    tabBarController.viewControllers = controllers
    window?.rootViewController = tabBarController
    let image1 = UIImage(named: "tshirtIcon.jpg")
    let image2 = UIImage(named: "measureTape.jpg")
    myVC1.tabBarItem = UITabBarItem(title: "tshirtIcon", image: image1, tag: 1)
    myVC2.tabBarItem = UITabBarItem(title: "measureTape", image: image2, tag:2)
    return true
}

现在显示的只是一个蓝屏,底部有一个灰色的标签。我认为问题可能出在线条上

    let myVC1: UIViewController = OutfitBuilderVC()
    let myVC2: UIViewController = MeasurementsVC()

我应该如何调用这些现有的 UIViewControllers?两个 UIViewController 都可以独立运行。我在单独的项目中将它们创建为唯一的 UIViewController。

【问题讨论】:

  • 如果你尝试:让 myVC1 = OutfitBuilderVC()
  • 这些控制器的视图是在哪里制作的?在代码中?
  • 您不只是使用默认应用模板制作标签栏并使用情节提要,是否有特定原因?
  • @MikeTaverne 我试过了,我得到了与灰色标签相同的蓝屏。
  • 这些行很好,如果您在代码中创建视图。我测试了你的代码,它对我来说很好。您可能需要向子控制器展示您在做什么。

标签: ios swift uiviewcontroller uitabbarcontroller


【解决方案1】:

您正在使用情节提要,对吗?

如果您正在使用情节提要,您只需用鼠标 ctrl 将一条线从您的 UITabBarController 拖到您的 UIViewControllers...(一行到 OutfitBuilderVC(),另一行到 MeasurementsVC())并在小黑options 将鼠标放在其中一个 UIViewController 上后出现的选项菜单,您只需单击“Relationship Segue -> view controllers”。

之后,您可以删除您在 AppDelegate 中编写的所有代码,因为情节提要会为您处理所有内容...您还可以在此处定义 vcs 的图标和标题...

你做的这个方法其实是,我想在老XCode 3的时候还没有storyboards的地方。但是今天你应该用故事板来做

【讨论】:

  • 我现在没有使用情节提要。我最终将拥有大约 11 或 12 个独立的视图控制器,其中一些功能是我无法使用情节提要的。代码工作得很好,它实际上是 UITabBarButtons 图像大小问题的组合,并且没有为每个视图控制器显式设置背景颜色。如果我以后决定将情节提要用于其他应用程序,我会记住这一点。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多