My application has a ViewController i.e. the first screen that opens when app is started. I added a UITabBarController to the application through mainstoryboard. I changed the title of UITabBarController to "myTabBarController". Now how do I access this TabBarController from the viewDidLoad function of already existing ViewController?
2 Answers
amazed! Why do you need view controller if you want to load UITabBarController from viewDidLoad function of view controller??? However solution can be like this: You can make segue: right click in view controller then drag to tabBarController and select segue "show".
But, better would be if you embed UITabBarController to view controller which loads your TabBarController at the beginning of your app. 1: Select View Controller 2: Go to [Editor] in menu bar of xcode 3: Select Embed In 4: Select Tab Bar Controller
and you are done :)
other solution would be: delete the ViewController from storyboard select tabBarController that you added. go to "attribute inspector" in left pane and select "Is initial View Controller"
ADDED 1. Give identifier to TabBarController like given by 張家齊 2. Add button in ViewController and make Action of it in ViewController's class (to make action. right click the added button and drag to ViewController class and select Action instead of Outlet.) 3. Now add following codes in that action:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("myTabBarController") //vc is instance of TabBarController.
self.presentViewController(vc, animated: true, completion: nil)
DONE :)
4 Comments
In AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let main = storyboard.instantiateViewControllerWithIdentifier("myTabBarController") as! myTabBarController
self.window!.rootViewController = main
self.window!.makeKeyAndVisible()
return true
}
And you should set Identifier of myTabBarController in your Main.storyboard.
3 Comments
let tabBarController = window?.rootViewController as? UITabBarController method. I had to eliminate the as! myTabBarController.