After successful login, i am trying to navigate to a tab bar controller. The code i have written is pasted below.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController = storyboard.instantiateViewControllerWithIdentifier("MenuViewController") as! MenuViewController
let nav = UINavigationController(rootViewController: viewController)
self.presentViewController(nav, animated: true, completion: nil)
It is redirecting to the first tab (MenuViewController) correctly. However, the remaining tab elements are not visible. Can anyone please suggest, if i can call the tabbarcontroller directly rather than the first viewcontroller?
So, here's an easy way to do this,
AppDelegate:
Let's say you want to set this up in your app delegate like so:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var navMain = UINavigationController()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
if (authenticated) {
var first = UIViewController(nibName: nil, bundle: nil)
var nav1 = UINavigationController()
nav1.viewControllers = [first]
var second = UIViewController(nibName: nil, bundle: nil)
var nav2 = UINavigationController()
nav2.viewControllers = [second]
var tabs = UITabBarController()
tabs.viewControllers = [nav1, nav2]
var LoginSignUpSplashPage = UIViewController(nibName: nil, bundle: nil)
navMain = UINavigationController()
self.window!.rootViewController = navMain;
navMain .setViewControllers([LoginSignUpSplashPage, tabs], animated: true)
} else {
var loginVC = UIViewController(nibName: nil, bundle: nil)
navMain = UINavigationController()
self.window!.rootViewController = navMain;
navMain .setViewControllers([loginVC], animated: true)
}
self.window?.makeKeyAndVisible();
return true
}
You decide to set this up because let's say you have some authentication method in the method above that executes an autologin after the user has already authenticated themselves right. Now, this will filter the users who are already authenticated and those who aren't, if they aren't authenticated, then send them to a view that will give them a login or sign up option.
So, the autologin code will set up a tabbar controller, place navigation controllers as the tabs, and then view controllers as the roots of these navigation controllers. Then you call to the main navigation Contoller "mainNav" and use the method "setViewControllers", you use this method so that you set up the root of the MAIN WINDOW of the app to the "mainNav" and then set up the "sign up or login screen" to the root of the "mainNav" and then the tab bar controler comes next, this will allow you to create a "log out" function that when you POP to the root view contorller of the app delegate after a log out, you are then presented with the "log in" or "sign up" screen with these buttons, thereby indicating to the user that they have indeed logged out. Okay, so now that we have that in stone, let's move onto the LoginViewController:
In the login view controller, after a successful authentication, you want to have the same set up you have in the AppDelegate, so you do this:
AFTER AUTHENTICATION:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let appDelegateVar = appDelegate.navMain
var first = UIViewController(nibName: nil, bundle: nil)
var nav1 = UINavigationController()
nav1.viewControllers = [first]
var second = UIViewController(nibName: nil, bundle: nil)
var nav2 = UINavigationController()
nav2.viewControllers = [second]
var tabs = UITabBarController()
tabs.viewControllers = [nav1, nav2]
var LoginSignUpSplashPage = UIViewController(nibName: nil, bundle: nil)
appDelegateVar .setViewControllers([LoginSignUpSplashPage, tabs], animated: true)
This same thing applies to the SignUpViewController, same thing, they sign up, you present the app delegate mainVC with the tabbarcontroller and the "splash screen sign up/login" view controller and you are set.
Now what you just did was you created a Navigation set up for your app, where the Splash screen is always at the ROOT of the mainNav and the Tabbar Controller is always at the second place spot. This will give you total control over logins and logouts and signups.
The way this works is that you are declaring mainNav as a property of the AppDelegate which gives you acccess to this property throughout the app, so from the LoginVc and the SignUpVc you can always call the mainNav from the AppDelegate to SET your viewcontrollers and thereby always control the entire heirarchy of the app.
This is a very advanced way to do things and it gets even more advanced than this if you want to go deeper, for example, you could all "presentViewController" using the mainNav property of the AppDelegate whenever you want to and you can execute "popToRootViewController" on the mainNav whenever you want to as well and the entire app is closed down except for the "splash screen" that presents new uesers with the "sign up" or "log in" button.
This is by far the best way to set up the front end processing of an app, it's the easiest once you get the hang of it and it gives you the ability to control the entire navigation stack without have to question where you are at in the app. You will more than likely have questions about this because I see you are using storyboards, so it's like this:
entry POINT ===> mainVC then the mainVC has the stuff above, this is how to set it up, you have the one mainVC and then a UINavigationController for each tab of the UITabBarController, and etc. etc.
Good luck!