1

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 2

2

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 :)

Sign up to request clarification or add additional context in comments.

4 Comments

Actually, I need to do some pre-processing from current ViewController--.viewDidLoad before actually presenting TabBarController, so I do want to keep my current viewcontroller.
SEE added section above in answer: you can do like that.
Awesome, this works .. Thanks Dari and also the user below. How do I select this as the correct answer, I don't have enough privilege to Vote up this post.
NP. ENJOY SWIFT CODING :)
1

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.

Like this image showing

3 Comments

How do I set identfier of myTabBarController in Main.storyboard? I set the Title as "myTabBarController"... which windows has this setting? Thanks
sorry, I am new to Swift/iOS programming, when I add the code you posted above to AppDelegate.swift then the TabBarController opens up as soon as App starts, but that's not what I want. I want to show the TabBarController only on a Button press. How can I do that?
Thanks. this saved me when i had a TabBarController but, i had to first present a login sreen before having access to the app's tabs. when a user logs out, the leave the tabs also. so, i couldn't acces the tab bar using the normal let tabBarController = window?.rootViewController as? UITabBarController method. I had to eliminate the as! myTabBarController.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.