在swift中添加没有nib文件的UIViewControllers到UITabBarController
我有两个名为MeasurementsVC.swift和OutfitBuilder.swift的UIViewController类。但是,我没有这些UIViewControllers的nib文件。我想将它们加载到UITabBarController中,以便在两个UIViewControllers之间切换。基于断the swift swift tutorial,我有我的AppDelegate文件在swift中添加没有nib文件的UIViewControllers到UITabBarController
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在单独的项目中创建它们。
那么你正在使用故事板是否正确?
如果你正在使用一个故事板,你只需要用鼠标从你的UITabBarController拖动一行到你的UIViewControllers ...(一行到OutfitBuilderVC(),另一个到MeasurementsVC())并且在小黑色选项在将鼠标放在其中一个UIViewControllers上,然后单击Relationship Segue - > view controllers之后出现的菜单。
之后,你可以删除所有你你写在AppDelegate中的代码,因为故事板为您处理一切......在那里,您还可以定义图标和您的VCS的标题...
这种方法你其实,我认为在老XCode 3的时候没有任何故事板在那里。但是今天你应该用故事板来做
我现在没有使用故事板。我将最终拥有大约11个或12个独立的视图控制器,并具有我无法用故事板弄清楚的一些功能。该代码工作得很好,它实际上是UITabBarButtons图像大小问题的组合,并且没有为每个viewcontroller明确设置backgroundcolor。如果我以后决定为其他应用程序使用故事板,我会记住这一点。谢谢。 – 2014-12-06 18:27:40
如果你尝试:let myVC1 = OutfitBuilderVC() – 2014-12-04 03:15:38
这些控制器的视图在哪里制作?在代码中? – rdelmar 2014-12-04 04:14:35
是否有一个特定的原因,你不只是从默认的应用程序模板和使用故事板制作标签栏? – Ian 2014-12-04 06:30:04