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中。

相关问题