airsoftFreak
airsoftFreak

Reputation: 1608

How to instantiate a navigation controller from another view controller?

My goal is whenever i click a button on a first view controller, then it will navigate to another controller which is a navigation controller.

firstViewController and secondViewController has no connection or anything.

Picture

enter image description here

I used this code

 @IBAction func buttonTapped(sender: UIButton) {

     let storyboard = UIStoryboard(name: "Main", bundle: nil)
     let vc = storyboard.instantiateViewControllerWithIdentifier("secondViewCtrl") as! SecondViewController
     self.presentViewController(vc, animated: true, completion: nil)

 }

The reason why i instatiate so that I could pass data like

vc.name = "Myname"

The problem with this code is that it doesn't present navigation bar and as well as the tab bar. What should I do to show both?

Updated question

 @IBAction func buttonTapped(sender: UIButton) {
    guard let tabBarController = tabBarController else { return }
            tabBarController.selectedIndex = 1


            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("trackYourGenie") as! TrackYourGenieViewController
            let navController = tabBarController.viewControllers![1]
            let secondViewController = navController.topViewController
            vc.name = "Myname"


 }

Upvotes: 2

Views: 5617

Answers (5)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

A safe approach:

guard let tabBarController = tabBarController else { return }
tabBarController.selectedIndex = 1

If you need to access to your tabBarController to pass datas you can do simply:

let navController = tabBarController.viewControllers[1]! as! UINavigationController
let secondViewController = navController.topViewController

Your method could be:

@IBAction func buttonTapped(sender: UIButton) {
    guard let tabBarController = tabBarController else { return }
    let navController = tabBarController.viewControllers[1]! as! UINavigationController
    let secondViewController = navController.topViewController as! SecondViewController
    secondViewController.name = "my name"
    tabBarController.selectedIndex = 1
}

Upvotes: 3

lubilis
lubilis

Reputation: 4160

I can see from your StoryBoard that you have a TabBarController. If your configuration is that and FirstViewController is on first tab and SecondViewController on second tab, you can just change TabBarController selectedIndex property:

@IBAction func buttonTapped(sender: UIButton) {

     tabBarController?.selectedIndex = 1
}

If you want to pass data to SecondViewController you can try one of these solutions:

  1. let controller = tabBarController.viewControllers[1] as SecondViewController! controller.data = "some data" This soultion could not work since SecondViewController is not ready yet.
  2. Create a singleton class where to save data, then retrieve data in SecondViewController viewDidLoad method
  3. Save data in UserDefaults, then retrieve data in SecondViewController viewDidLoad method (bad solution if information doesn't have to be persistent)
  4. Extend UITabBarController and use it, create a custom var in tabBarController, put data in that variable and then retrieve data in SecondViewController viewDidLoad method

Then clear data if needed.

Upvotes: 1

Jeet
Jeet

Reputation: 5659

You are instantiating the view controller hence you wouldn't get the navigation bar. To get the navigation bar please instantiate navigation controller and since the second view is only child you would get the second view by default.

@IBAction func buttonTapped(sender: UIButton) {

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
 let vc = storyboard.instantiateViewControllerWithIdentifier("Navigation Controller Id") as! UINavigationController
 self.presentViewController(vc, animated: true, completion: nil)

}

The above code should give you the navigation bar.

Upvotes: 4

Amit Singh
Amit Singh

Reputation: 2698

Navigation will not work from firstViewController as to navigate something we need UINavigationController.

Try Like This

enter image description here

Upvotes: 0

AnthoPak
AnthoPak

Reputation: 4391

In your case, the following code should be sufficient :

self.tabBarController.selectedIndex = 1 

As UITabBarController is your rootViewController, you can access it with self.tabBarController. You don't have to instantiate UINavigationController as it is in the storyboard.

Upvotes: 1

Related Questions