1

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.

8
  • is it Viewcontroller-1 to ViewController-3 to TargetViewController as per your diagram? Commented Dec 5, 2018 at 13:39
  • From ViewController 1 straight to TargetViewController. The button tap would happen in ViewController 1. Commented Dec 5, 2018 at 13:46
  • 1
    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? Commented Dec 5, 2018 at 13:52
  • 1
    Hey, @fs_tigre please check my answer may be it will help you. Commented Dec 5, 2018 at 13:57
  • 2
    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. Commented Dec 5, 2018 at 14:00

3 Answers 3

3

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.

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

3 Comments

Sorry but I don't fully understand the first two lines of your code, is this a singleton of ViewController 1 or TargetViewController? I cannot make it work.
@fs_tigre it is the singleton class of TabbarController Where all four controllers are added, so when I need TabController in any controller so I can use this. Let me explain you in Details : 1)TabController.shared.tabcontroller.selectedIndex = 1: This line will redirect to you on ViewController3, In your case you have to pass your index. 2) let navigation = TabController.shared.tabcontroller.selectedViewController?.navigationController : This line will you give you NavigationController object of selected View controller. And after you only have to only push to TargertVC
Thanks you for the explanation, I will give it a try and let you know. I like this because it seem to be less code but I still not sure how is method tabcontroller implemented.
2

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

9 Comments

I couldn't make this work, no error it just doesn't do anything. Thanks.
@fs_tigre I have tried to edit my answer please verify it is helpful to you or not. let me know if you stuck anywhere
It worked thanks a lot. Quick question, is there a way to accomplish the same thing but without having the class method in the TargetViewController? In other words, I would like to have all the code in the calling method.
@fs_tigre yes it is possible just write let vc = self.storyboard.instantiateViewController(withIdentifier: "yourIdentifer") as! TargetViewController instead of let vc = TargetViewController.viewController()
@fs_tigre it is used for code re usability . Now suppose you have to call This vc from 5 different places then you have to write same code at 5 different places. And other example is you change the storyboard identifier then you have to change each and every place. Hope it is helpful
|
1

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

1 Comment

This does take me to TargetViewController, the issue is that it doesn't show the back button, as @ Denislava Shentova mentioned it.

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.