1

我對swift相當陌生,並試圖使用Apiary使用TMDB創建電影數據庫應用程序。以編程方式將視圖控制器分配給標籤欄控制器

我爲一個類別創建了視圖控制器的佈局。不過,我打算有多個類別,但它們之間的唯一區別是我從API中檢索的數據。例如,「now_playing」鍵獲得正在播放的列表,「top_rated」鍵獲得最高評價的電影列表。當我最初這樣做時,我在AppDelegate.swift中創建了標籤欄控制器,並通過使用方法instantiateViewControllerWithIdentifier從故事板以編程方式讓它們添加到ViewController中。最後,我通過這樣做,將TabBarController的視圖控制器設置爲兩個ViewControllers:tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]

這工作正常。稍後爲了添加可定製性,我嘗試使用容器視圖創建一個滑出菜單。 Story board for root View Controller and Menu which is a ViewController with a tableview inside of it.

此時我無法將以編程方式創建的Tab Bar Controller嵌入到ContainerViewController右側的大容器視圖中。由於我無法弄清楚如何以編程方式做到這一點,我試圖在main.storyboard中創建一個TabBarController,並給它一個StoryboardID,並嘗試將它設置爲AppDelegate.swift中的變量TabBarController。

Tried embedding TabBarController in container

var tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController 

後,當我試圖修改的TabBar並指定以前創建ViewControllers到TabBarController它根本不起作用。

tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController] 
tabBarController.tabBar.barStyle = .Black 
tabBarController.tabBar.tintColor = UIColor.orangeColor() 

我不確定爲什麼這不起作用請幫我一把。

Simulated Application, the TabBarController is not updating from the code in AppDelegate.swift

參考

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 
} 
+0

定義「根本不起作用」。 –

+0

只是不起作用,這意味着標籤欄控制器不會改變。模擬的圖片就是結果。我分配給標籤欄控制器的視圖控制器不會顯示,並且標籤欄顏色不會更改。 –

+0

更好地將您的項目上傳到Dropbox。 –

回答

1

AppDelegate.Swift didFinishLaunchingWithOptions功能一開始我並沒有對如何解決這個問題的想法,但有人幫了我,現在我懂了工作。

而不是控制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() 

    } 
} 

我相信這有許多工作要做,因爲TabBarController在故事板實例化,幷包含在一個containerView那就是ContainerViewController內。所以,控制它的方法將在prepareForSegue方法中而不是在AppDelegate.swift中。

相關問題