gpt4 book ai didi

swift - 以编程方式将 View Controller 分配给选项卡栏 Controller

转载 作者:行者123 更新时间:2023-11-28 06:51:28 26 4
gpt4 key购买 nike

我对 swift 还很陌生,正在尝试使用 Apiary 的 TMDB 创建电影数据库应用程序。

我已经为一个类别创建了 View Controller 的布局。但是,我计划有多个类别,但它们之间的唯一区别是我从 API 检索的数据。例如,“now_playing”键获取正在播放的列表,“top_rated”键获取评分最高的电影列表。当我最初这样做时,我在 AppDelegate.swift 中创建了选项卡栏 Controller ,并通过使用方法 instantiateViewControllerWithIdentifier 以编程方式从 Storyboard 中获取它们来将 ViewController 添加到其中。最后,我通过执行以下操作将 TabBarController 的 View Controller 设置为两个 ViewController:tabBarController.viewControllers = [nowPlayingNavigationController, topRatedNavigationController]

这工作正常。后来为了添加可定制性,我尝试使用容器 View 创建一个滑出式菜单。 Story board for root View Controller and Menu which is a ViewController with a tableview inside of it.

此时我无法将以编程方式创建的 Tab Bar Controller 嵌入到 ContainerViewController 右侧的大型容器 View 中。由于我无法弄清楚如何以编程方式执行此操作,因此我尝试在 main.storyboard 中创建一个 TabBarController 并为其提供 StoryboardID 并尝试将其设置为 AppDelegate.swift 中的变量 TabBarController。

Tried embedding TabBarController in container

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

稍后,当我尝试修改 TabBar 并将之前创建的 ViewController 分配给 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

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
}

最佳答案

我最初不知道如何解决这个问题,但有人帮助了我,现在我开始工作了。

不是在 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 在 StoryBoard 中实例化并包含在 ContainerViewController 内部的 containerView 中。因此,控制它的方法是在 prepareForSegue 方法中而不是在 AppDelegate.swift 中。

关于swift - 以编程方式将 View Controller 分配给选项卡栏 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com