1

I'm trying to segue to a VC that is not the Root VC.

@IBAction func composeJournalTapped(sender: AnyObject) {
    self.tabBarController?.selectedIndex = 2

    let journalVC = self.storyboard?.instantiateViewControllerWithIdentifier("JournalEntryTableViewController") as! JournalEntryTableViewController

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


}

I know the obvious answer would be to send a push segue to that VC, but that does not switch tabs. I want to switch tabs and also switch to a viewController within that tab which isn't the root. The above code strictly switches tabs. In other words, I want to switch from index 1 to index 2, then push segue from index 2 rootVC to destinationVC.

1 Answer 1

1

So you need code that does what you want. If the 2nd tab in your tab bar controller is a navigation controller, cast it to the appropriate type and implement an interface that lets you present the appropriate view controller.

I can't tell you the details of how to do that because I don't know your app's structure. Will the VC in question already be on tab 2's navigation stack? Will it always NOT be on the stack, so a push segue is the right thing to do? Is either case possible? You need to map out the cases and design code that deals with all the possoble case.

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

4 Comments

The destination view controller is the 2nd VC on the navigation stack of the destination tab. It will always be there. Instantiating the destinationVC will take it out of the navigation stack, which is not what I want.
So make your IBAction first se the tab bar controller's selectedIndex to the desired index, and then get a pointer to that tab's view controller (your navigation controller) cast it to the appropriate type, and use a call to `popToViewController:animated' to pop to that view controller on the stack.
@IBAction func composeJournalTapped(sender: AnyObject) { self.tabBarController?.selectedIndex = 2 self.navigationController!.popToViewController(navigationController!.viewControllers[2] as! JournalEntryTableViewController, animated: false) } this causes a crash...index out of range
Assuming that the tab at Index 2 contains a navigation controller and that's the one you want to pop, you need to tell THAT navigation controller to pop, not self.navigationController.

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.