将没有nib文件的现有UIViewControllers添加到swift中的UITabBarController

时间:2014-12-04 02:11:48

标签: ios swift uiviewcontroller uitabbarcontroller

我有两个名为MeasurementsVC.swift和OutfitBuilder.swift的UIViewController类。但是,我没有这些UIViewControllers的nib文件。我想将它们加载到UITabBarController中,这样我就可以在两个UIViewControllers之间切换。基于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?两个UIViewControllers都可以自己运行。我在单独的项目中创建它们作为唯一的UIViewController。

1 个答案:

答案 0 :(得分:0)

你正在使用故事板是否正确?

如果你正在使用故事板,你只需要用你的鼠标从你的UITabBarController拖动一行到你的UIViewControllers ......(一行到OutfitBuilderVC(),另一行到MeasurementsVC())和小黑options将鼠标放在其中一个UIViewControllers之后出现的菜单,只需单击Relationship Segue - >查看控制器。

之后你可以删除你在AppDelegate中编写的所有代码,因为故事板会为你处理一切......在那里你也可以定义你的vcs的图标和标题......

你正在做的这个方法实际上,我认为在旧的XCode 3中没有故事板的地方。但是今天你应该用故事板来做这件事