Kevin Rajan
Kevin Rajan

Reputation: 825

How to make tab bar controller update?

I'm rather new to swift and I am currently building an application that shows movies and TV shows that are in theatres / just airing.

Initially I had it so that there are only 2 tabs in the tab bar controller and it shows newest movies and recently aired TV shows. In order to enhance user experience I created a menu through which the use could pick up to 4 different categories (e.g. Top rated movies, newly released movies, popular movies and so on). I have created an array so that the tab bar controller knows where to get the data from to see the movies and the TV shows. When the user selects the new categories, the array is updated, but the tab bar controller does not update its tabs. When the app is restarted though, the tabs reflect the newly selected categories.

I know if this were a tableview I could call tableview.reload() but I'm not sure how to do this for a tab bar controller. Please help me out and thanks in advance!

Here is the link to my project

Upvotes: 0

Views: 804

Answers (1)

Arbaz Shaikh
Arbaz Shaikh

Reputation: 407

Kevin you can make one function in appDelegate and make the tabbar a rootview controller

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

You can write this code in that function and make it root viewcontroller and make the title change,

Or

in your Tabbarviewcontroller.swift you can use postnotification and update it accordingly.

Upvotes: 1

Related Questions