How to make tab bar controller update? - swift

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

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.

Related

Swift iOS Dynamic Tab bar from json response

I'm new to iOS/Swift. My application is using a json data and I have to create tab bar using the json response. I mean, I get Array of Title from json and I have to create tab bar items based on that array. The Array data/count may change and the app should display the tab bar accordingly. I am trying to create the tab bar programatically without storyboards (as this is huge tab bar)
So far, I have tried the below code -
func tabBarCustom() {
let tt = UITabBarController()
var array1 = [UIViewController]()
var controller1 = UIViewController()
for i in 0..<navgTitle.count {
controller1 = UIViewController(nibName: "WeatherViewController", bundle: nil)
controller1.title = navgTitle[i]
controller1.tabBarItem = UITabBarItem(title: navgTitle[i], image: .none, tag: 1)
array1.append(controller1)
}
print(array1)
tt.viewControllers = array1
self.view.addSubview(tt.view)
}
The above code is failing saying - Could not load NIB in bundle: 'NSBundle' with name 'WeatherViewController'
I am not sure how to create multiple view controllers automatically using the title array, taking title as name of the view controller. is this possible? and how to loop in the array to create view controllers for each tab bar item
please help.Thank you
Are you using nibs? If so, your project can't find the nib file, and you should check out this: Could not load NIB in bundle: 'NSBundle'. Or are you using Storyboard? If you are trying to initialize from the storyboard, you'll do something like this:
let vc = sb.instantiateViewController(withIdentifier: "WeatherViewController")
(just be sure to add WeatherViewController as the view controller's identifier).
OR are you using code? If you are trying to create your view controller from code, you simply need to do WeatherViewController().
As for the tab bar, you're in the right direction it's just that creating the view controller is failing.

clearing variables of root view controller when switching tabs programmatically

I have a storyboard/root viewcontroller as the third tab on my app. On that tab the user selects an image, and writes a caption, which are stored as variables. From the root viewcontroller the user is taken to a preview screen via a segue which passes the variables, i.e. image and text. From this screen the user posts the object. From the post button, I am popping the viewcontroller, and programatically navigating to the first tab of my app. That all works, but the problem is when I then navigate back to the third tab, the stored variables are still there, and I'd like them to be cleared out.
I've seen many posts on keeping the variables, but none on how to reset them. I've tried to use viewWillDissapear but I don't want them cleared when I use the segue because I want the user to be able to go back and make some changes if needed.
UPDATED CODE WITH CORRECT ANSWER
#IBAction func postButtonPressed(_ sender: Any) {
PostFunction.createPost(image: self.postImage, postText: self.hashtag) { (true) in
self.tabBarController?.selectedIndex = 0
let tab3 = self.tabBarController!.viewControllers![2] as! UINavigationController
let vc = tab3.viewControllers.first as! PostHomeVC
vc.clear()
self.navigationController?.popViewController(animated: false)
}
}
Any suggestions on how to "reset" the rootviewcontroller from the above method?
Thanks!
I am popping the viewcontroller, and programatically navigating to the first tab of my app
before you do the programmatic switch to the first tab do
let tab3 = self.tabBarController.viewControllers[2] as! UINavigationController
let vc = tab3.viewControllers.first as! VCName
vc.clear()
or
let vc = self.navigationController!.viewControllers.first as! VCName
vc.clear()
and write that clear method inside the vc as you need

Tab bar disappeared when back to initial view controller

i have 2 view controllers then tab bar controller connected to 3 navigation controller , each navigation connected to multiple views .
The point that from one of these views that is connected to one of navigation controllers , i want to back again to the start screen , it good by this 2 lines :
let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController")
UIApplication.shared.keyWindow?.rootViewController = loginViewController
but after i do this and want to navigate again to the tab bar its disappeared from the view . I used this to navigate to tab bar :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let viewTabController = self.storyboard?.instantiateViewController(withIdentifier :"MainTabBarController") as! MainTabBarController
viewTabController.selectedIndex = 1
appDelegate.window?.rootViewController = viewTabController
Thanks in advance.
When you use instantiateViewController you are making new instances of view controllers in memory while there is no need in your case , you have created them before. so you can dissmis your tabBar and go back to first screen like this (initial viewcontroller must be embeded into a navigation controller) :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = appDelegate.window?.rootViewController as! UINavigationController
navigationController.dismiss(animated: true, completion: nil)
for bringing back tabBar you can either save its instance in your app delegate and present it or use a storyboard segue to present it modally
:)

Can it be non rootviewcontroller?

I'm implementing a drawer layout design in an app.
But my app starts with a small screen with an animated logo (simple HTML5 animation), then a login screen (g+ and Facebook), then the main screen where I'm implementing the MMDrawerController.
The question is in my AppDelegate:
window?.rootViewController = centerContainer
window?.makeKeyAndVisible()
So the app start in this screen. Is it possible to not make rootviewcontroller the center container and still using MMDrawerController?
I need to add MMDrawerController to my third viewcontroller in my app
But, in order to MMDrawerController to work, it requires to be the rootViewController
I allready tried to add to my first ViewController an Empty MMDrawerLayout but, then, the third controller no longer works
//global var
var centerContainer : MMDrawerController?
// then the appdelegate
let rootViewController = self.window!.rootViewController
let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("GaleriaPeliculas")
let leftViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("LeftSideViewController")
let leftSideNav = UINavigationController(rootViewController: leftViewController)
let centerSideNav = UINavigationController(rootViewController: centerViewController)
//And here is the problem,
window?.rootViewController = centerContainer //how can it work without this line??
window?.makeKeyAndVisible()
Is it possible to not make rootviewcontroller the center container and still using MMDrawerController?
No. You need to keep MMDrawerController as the rootViewController if you still want to make use of the left/right drawers. It's no different than a UITabBarController in the sense that it contains multiple view controllers to be conditionally displayed.

How to show a tab bar controller along with sidebar menu after login in swift IOS9

I want to show a tab bar controller along with sidebar menu in homepage (Main Page). After a successful login if I call the delegate to main page with sidebar menu then tab bar does not load and if i call the tab bar controller after login then sidebar menu does not work, I am confused on how to call the sidebar menu delegate and tab bar controller together so it may show both together (Sidebar menu and tab bar together on main page) in swift ios9.
Here is my code in App Delegate that build my sidebar menu
func buildNavigationDrawer()
{
// Navigate to Protected page
let mainStoryBoard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
// Create View Controllers
let mainPage = mainStoryBoard.instantiateViewControllerWithIdentifier("MainPageViewController") as! MainPageViewController
let leftSideMenu:LeftSideViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideViewController
let rightSideMenu:RightSideViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("RightSideViewController") as! RightSideViewController
let mainPageNav = UINavigationController(rootViewController:mainPage)
let leftSideMenuNav = UINavigationController(rootViewController:leftSideMenu)
let rightSideMenuNav = UINavigationController(rootViewController:rightSideMenu)
drawerContainer = MMDrawerController(centerViewController: mainPageNav, leftDrawerViewController: leftSideMenuNav, rightDrawerViewController: rightSideMenuNav)
drawerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView
drawerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView
window?.rootViewController = drawerContainer
}
http://i.stack.imgur.com/05AQz.png
Click Here to view Image
Thank you.