我有两个 UIViewController 类,分别称为 MeasurementsVC.swift 和 OutfitBuilder.swift。但是,我没有这些 UIViewController 的 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()
我应该如何调用这些现有的 UIViewController?两个 UIViewController 都可以独立运行。我在单独的项目中将它们创建为唯一的 UIViewController。
最佳答案
那么您正在使用 Storyboard,对吗?
如果你正在使用一个 Storyboard,你只需要用鼠标按住 ctrl 从你的 UITabBarController 拖一条线到你的 UIViewControllers...(一条线到 OutfitBuilderVC(),另一条线到 MeasurementsVC())并且在小黑释放鼠标后出现的选项菜单将鼠标悬停在其中一个 UIViewController 上,您只需单击 Relationship Segue -> view controllers。
在那之后你可以删除你在 AppDelegate 中编写的所有代码,因为 Storyboard会为你处理一切......在那里你还可以定义你的 vcs 的图标和标题......
你正在做的这个方法实际上是,我认为在没有 Storyboard的旧 XCode 3 中。但是今天你应该用 Storyboard来做
关于ios - 快速将没有 nib 文件的现有 UIViewControllers 添加到 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27285109/