1
votes

I have the following ViewController structure, see image below.

Whenever I want to move from ViewController 1 to any of the other main controllers I use self.tabBarController?.selectedIndex = indexNumber

// for instance, this would take me to ViewController 3
self.tabBarController?.selectedIndex = 2

Based on the picture below, how can I go from ViewController 1 to TargetViewController programmatically when a button is tapped in ViewController 1?

FYI - In the picture below I'm showing a button in ViewController 3 this is just to show the storyboard structure, the actual button-tap will happen in ViewController 1

enter image description here

EDIT:

Here is how you do it based on Prashant Tukadiya's answer.

ViewController 1

In ViewController 1 add the following in your tap event.

    self.tabBarController?.selectedIndex = 2
    let nav = (self.tabBarController?.viewControllers?[2] as? UINavigationController)
    let vc =  TargetViewController.viewController()
    nav?.pushViewController(vc, animated: true)

TargetViewController

  1. In your TargetViewController add the following class method.

    class func viewController() -> TargetViewController { let storyboard = UIStoryboard(name: "Main", bundle: nil) return storyboard.instantiateViewController(withIdentifier: "targetViewControllerID") as! TargetViewController }

  2. Add targetViewControllerID in the Storyboard ID field.

3
is it Viewcontroller-1 to ViewController-3 to TargetViewController as per your diagram? - Suhit Patil
From ViewController 1 straight to TargetViewController. The button tap would happen in ViewController 1. - fs_tigre
Yes, but if you want to go back from the TargetViewController (<Back navigation bar button), should you stay in the first tab hierarchy or you should be in the hierarchy of tab 3? - Denislava Shentova
Hey, @fs_tigre please check my answer may be it will help you. - Sumit singh
If that's the case, @Sumitsingh is the way to go :). 1. Select 3rd tab. 2. Get nav controller for 3rd tab. 3. Create TargetVC. 4. Push to nav controller for 3rd tab. Edit: You might need to call popToRoot on the navigation controller to avoid messes. - Denislava Shentova

3 Answers

3
votes

I was facing same before a couple of days and finally, I got a solution maybe this will help you.

    TabController.shared.tabcontroller.selectedIndex = 1
    let navigation  = TabController.shared.tabcontroller.selectedViewController?.navigationController

    let SavedAddress = self.storyboard?.instantiateViewController(withIdentifier: "SavedAddressVC") as! SavedAddressVC
    navigation?.pushViewController(SavedAddress, animated: true)

I have used the same solution. and this is the working code for me.

2
votes

You can directly give identifier to the TargetViewController from storyboard and load from storyboard then and push or present it.

like add this method in your TargetViewController

class func viewController () ->  TargetViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    return storyboard.instantiateViewController(withIdentifier: "yourIdentifer") as! TargetViewController
}

and on tap event

     let vc =  TargetViewController.viewController()

    self.navigationController?.pushViewController(vc, animated: true)

EDIT

After reading comments got clear idea about your requirements

On button action from ViewController1, You want to goto TargetViewController but when press back you want back to viewController 3

Select particular index first

   self.tabBarController?.selectedIndex = 2

After that you need to grab UINavigationController

let nav = (self.tabBarController?.viewControllers?[2] as? UINavigationController)

then Push ViewController

    let vc =  TargetViewController.viewController()

    nav?.pushViewController(vc, animated: true)

Note: Don't forgot add identifier to storyboard to TargetViewController and also add class func viewController () -> TargetViewController method to TargetViewController

Hope it is helpful

1
votes

In action function of a button in ViewController 1 , you can use this :

     func presentMethod(storyBoardName: String, storyBoardID: String) {
        let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: storyBoardID)
        self.definesPresentationContext = true
        self.present(newViewController, animated: true, completion: nil)
    }

//usage

 presentMethod(storyBoardName: "Main", storyBoardID: "TargetViewController")