I am fairly new to swift and am trying to create a movie database application using TMDB from Apiary.
我对swift很新,我正在尝试使用Apiary的TMDB创建一个电影数据库应用程序。
I have created the layout of the view controllers for one category. However, I am planning to have multiple categories, the but only difference between them is the data I retrieve from the API. For example "now_playing" key gets the now playing list and the "top_rated" key gets the top rated list of movies. When I was initially doing this I created the tab bar controller in AppDelegate.swift and added the ViewControllers to it by getting them programmatically from the storyboard with the method instantiateViewControllerWithIdentifier. Then finally the I set the TabBarController's view controllers to the two ViewControllers by doing this: tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
我已经为一个类别创建了视图控制器的布局。但是,我打算有多个类别,但它们之间唯一的区别是我从API检索的数据。例如,“now_playing”键获取正在播放的列表,“top_rated”键获得最受好评的电影列表。当我最初这样做时,我在AppDelegate.swift中创建了标签栏控制器,并通过使用方法instantiateViewControllerWithIdentifier以编程方式从故事板中获取它们,将ViewControllers添加到它。然后我通过这样做将TabBarController的视图控制器设置为两个ViewControllers:tabBarController.viewControllers = [nowPlayingNavigationController,topRatedNavigationController]
This was working fine. Later to add customizability I tried creating a slide out menu using container views.
这工作正常。后来为了添加自定义功能,我尝试使用容器视图创建一个滑出菜单。
At this point I was unable to embed the programmatically created Tab Bar Controller into the large container view on the right side of the ContainerViewController. Since I could not figure out how to programmatically do this, I attempted to create a TabBarController in main.storyboard and gave it a StoryboardID and tried setting it to a variable TabBarController in AppDelegate.swift.
此时,我无法将以编程方式创建的Tab Bar Controller嵌入到ContainerViewController右侧的大容器视图中。由于我无法弄清楚如何以编程方式执行此操作,我尝试在main.storyboard中创建一个TabBarController,并为其提供了StoryboardID,并尝试将其设置为AppDelegate.swift中的变量TabBarController。
var tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
later when I try to modify the TabBar and assign the previously created ViewControllers to the TabBarController it simply does not work.
稍后,当我尝试修改TabBar并将之前创建的ViewControllers分配给TabBarController时,它根本不起作用。
tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
tabBarController.tabBar.barStyle = .Black
tabBarController.tabBar.tintColor = UIColor.orangeColor()
I am not sure why this isn't working please help me out.
我不确定为什么这不起作用请帮助我。
AppDelegate.Swift didFinishLaunchingWithOptions function for reference
AppDelegate.Swift didFinishLaunchingWithOptions函数供参考
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nowPlayingNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let nowPlayingViewController = nowPlayingNavigationController.topViewController as! MoviesViewController
nowPlayingViewController.endpoint = "now_playing"
nowPlayingNavigationController.tabBarItem.title = "Now Playing"
nowPlayingNavigationController.tabBarItem.image = UIImage(named: "popular")
nowPlayingNavigationController.navigationBar.barStyle = .BlackTranslucent
let topRatedNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let topRatedViewController = topRatedNavigationController.topViewController as! MoviesViewController
topRatedViewController.endpoint = "top_rated"
topRatedNavigationController.tabBarItem.title = "Top Rated"
topRatedNavigationController.tabBarItem.image = UIImage(named: "topRated")
topRatedNavigationController.navigationBar.barStyle = .BlackTranslucent
var tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
//tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
tabBarController.tabBar.barStyle = .Black
tabBarController.tabBar.tintColor = UIColor.orangeColor()
//let containerViewController = storyboard.instantiateViewControllerWithIdentifier("ContainerViewController") as! ContainerViewController
//let mainContainerView = containerViewController.mainContainerView
//window?.rootViewController = tabBarController
//window?.makeKeyAndVisible()
return true
}
1 个解决方案
#1
1
I initially had no idea of how to fix this problem but someone helped me out and now I got it working.
我最初不知道如何解决这个问题,但有人帮助我,现在我得到了它的工作。
Instead of controlling the TabBarController in the AppDelegate.swift it has to be done in ContainerViewController.swift and it should be done in the prepareForSegue method.
而不是在AppDelegate.swift中控制TabBarController,它必须在ContainerViewController.swift中完成,它应该在prepareForSegue方法中完成。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.destinationViewController.isKindOfClass(UITabBarController) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nowPlayingNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let nowPlayingViewController = nowPlayingNavigationController.topViewController as! MoviesViewController
nowPlayingViewController.endpoint = "now_playing"
nowPlayingNavigationController.tabBarItem.title = "Now Playing"
nowPlayingNavigationController.tabBarItem.image = UIImage(named: "popular")
nowPlayingNavigationController.navigationBar.barStyle = .BlackTranslucent
let topRatedNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let topRatedViewController = topRatedNavigationController.topViewController as! MoviesViewController
topRatedViewController.endpoint = "top_rated"
topRatedNavigationController.tabBarItem.title = "Top Rated"
topRatedNavigationController.tabBarItem.image = UIImage(named: "topRated")
topRatedNavigationController.navigationBar.barStyle = .BlackTranslucent
let tabVC = segue.destinationViewController as! UITabBarController
tabVC.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
tabVC.tabBar.barStyle = .Black
tabVC.tabBar.tintColor = UIColor.orangeColor()
}
}
I believe this has to be done since the TabBarController is instantiated in StoryBoard and is contained in a containerView that is inside of the ContainerViewController. So, the way to control it would be inside the prepareForSegue method rather than in AppDelegate.swift.
我相信这必须要做,因为TabBarController在StoryBoard中实例化,并包含在ContainerViewController内部的containerView中。因此,控制它的方法将在prepareForSegue方法中,而不是在AppDelegate.swift中。
#1
1
I initially had no idea of how to fix this problem but someone helped me out and now I got it working.
我最初不知道如何解决这个问题,但有人帮助我,现在我得到了它的工作。
Instead of controlling the TabBarController in the AppDelegate.swift it has to be done in ContainerViewController.swift and it should be done in the prepareForSegue method.
而不是在AppDelegate.swift中控制TabBarController,它必须在ContainerViewController.swift中完成,它应该在prepareForSegue方法中完成。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.destinationViewController.isKindOfClass(UITabBarController) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nowPlayingNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let nowPlayingViewController = nowPlayingNavigationController.topViewController as! MoviesViewController
nowPlayingViewController.endpoint = "now_playing"
nowPlayingNavigationController.tabBarItem.title = "Now Playing"
nowPlayingNavigationController.tabBarItem.image = UIImage(named: "popular")
nowPlayingNavigationController.navigationBar.barStyle = .BlackTranslucent
let topRatedNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let topRatedViewController = topRatedNavigationController.topViewController as! MoviesViewController
topRatedViewController.endpoint = "top_rated"
topRatedNavigationController.tabBarItem.title = "Top Rated"
topRatedNavigationController.tabBarItem.image = UIImage(named: "topRated")
topRatedNavigationController.navigationBar.barStyle = .BlackTranslucent
let tabVC = segue.destinationViewController as! UITabBarController
tabVC.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
tabVC.tabBar.barStyle = .Black
tabVC.tabBar.tintColor = UIColor.orangeColor()
}
}
I believe this has to be done since the TabBarController is instantiated in StoryBoard and is contained in a containerView that is inside of the ContainerViewController. So, the way to control it would be inside the prepareForSegue method rather than in AppDelegate.swift.
我相信这必须要做,因为TabBarController在StoryBoard中实例化,并包含在ContainerViewController内部的containerView中。因此,控制它的方法将在prepareForSegue方法中,而不是在AppDelegate.swift中。