如何更新标签栏控制器?
How to make tab bar controller update?
我是 swift 的新手,我目前正在构建一个应用程序来显示正在影院/刚刚播出的电影和电视节目。
最初我有它,所以标签栏控制器中只有 2 个标签,它显示最新的电影和最近播出的电视节目。为了增强用户体验,我创建了一个菜单,用户可以通过该菜单选择最多 4 个不同的类别(例如评分最高的电影、新发行的电影、热门电影等)。
我创建了一个数组,以便选项卡栏控制器知道从哪里获取数据以观看电影和电视节目。当用户选择新类别时,数组会更新,但选项卡栏控制器不会更新其选项卡。但是,当应用程序重新启动时,选项卡会反映新选择的类别。
我知道如果这是我可以调用 tableview.reload() 的表格视图,但我不确定如何为选项卡栏控制器执行此操作。请帮助我并提前致谢!
凯文,你可以在 appDelegate 中创建一个函数,并使标签栏成为根视图控制器
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 = "YOUR SELECTED TITLE"
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 = "YOUR SELECTED TITLE"
topRatedNavigationController.tabBarItem.image = UIImage(named: "topRated")
topRatedNavigationController.navigationBar.barStyle = .BlackTranslucent
window?.rootViewController = tabBarController
您可以在该函数中编写此代码并将其设置为根 viewcontroller 并更改标题,
或
在您的 Tabbarviewcontroller.swift 中,您可以使用 postnotification 并相应地更新它。
我是 swift 的新手,我目前正在构建一个应用程序来显示正在影院/刚刚播出的电影和电视节目。
最初我有它,所以标签栏控制器中只有 2 个标签,它显示最新的电影和最近播出的电视节目。为了增强用户体验,我创建了一个菜单,用户可以通过该菜单选择最多 4 个不同的类别(例如评分最高的电影、新发行的电影、热门电影等)。 我创建了一个数组,以便选项卡栏控制器知道从哪里获取数据以观看电影和电视节目。当用户选择新类别时,数组会更新,但选项卡栏控制器不会更新其选项卡。但是,当应用程序重新启动时,选项卡会反映新选择的类别。
我知道如果这是我可以调用 tableview.reload() 的表格视图,但我不确定如何为选项卡栏控制器执行此操作。请帮助我并提前致谢!
凯文,你可以在 appDelegate 中创建一个函数,并使标签栏成为根视图控制器
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 = "YOUR SELECTED TITLE"
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 = "YOUR SELECTED TITLE"
topRatedNavigationController.tabBarItem.image = UIImage(named: "topRated")
topRatedNavigationController.navigationBar.barStyle = .BlackTranslucent
window?.rootViewController = tabBarController
您可以在该函数中编写此代码并将其设置为根 viewcontroller 并更改标题,
或
在您的 Tabbarviewcontroller.swift 中,您可以使用 postnotification 并相应地更新它。