我對swift相當陌生,並試圖使用Apiary使用TMDB創建電影數據庫應用程序。以編程方式將視圖控制器分配給標籤欄控制器
我爲一個類別創建了視圖控制器的佈局。不過,我打算有多個類別,但它們之間的唯一區別是我從API中檢索的數據。例如,「now_playing」鍵獲得正在播放的列表,「top_rated」鍵獲得最高評價的電影列表。當我最初這樣做時,我在AppDelegate.swift中創建了標籤欄控制器,並通過使用方法instantiateViewControllerWithIdentifier從故事板以編程方式讓它們添加到ViewController中。最後,我通過這樣做,將TabBarController的視圖控制器設置爲兩個ViewControllers:tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
這工作正常。稍後爲了添加可定製性,我嘗試使用容器視圖創建一個滑出菜單。
此時我無法將以編程方式創建的Tab Bar Controller嵌入到ContainerViewController右側的大容器視圖中。由於我無法弄清楚如何以編程方式做到這一點,我試圖在main.storyboard中創建一個TabBarController,並給它一個StoryboardID,並嘗試將它設置爲AppDelegate.swift中的變量TabBarController。
var tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
後,當我試圖修改的TabBar並指定以前創建ViewControllers到TabBarController它根本不起作用。
tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]
tabBarController.tabBar.barStyle = .Black
tabBarController.tabBar.tintColor = UIColor.orangeColor()
我不確定爲什麼這不起作用請幫我一把。
參考
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
}
定義「根本不起作用」。 –
只是不起作用,這意味着標籤欄控制器不會改變。模擬的圖片就是結果。我分配給標籤欄控制器的視圖控制器不會顯示,並且標籤欄顏色不會更改。 –
更好地將您的項目上傳到Dropbox。 –