10

Hello Im trying to segue from a modal to a tab bar view controller without losing the tab bar? I know the question is short, but this is all I'm asking.

Scenario: I have a Tab Bar View Controllers, A and B. B modals to C view controller. Then I want to return to View Controller A.

Swift please :D

MasterWizard
  • 767
  • 2
  • 13
  • 42

1 Answers1

15

Here is my example of how to do this. In my setup, I choose the yellow ViewController from the tab, then press Go! which modally presents the white ViewController. Pressing Exit returns to the green ViewController.

Storyboard overview


To set this up, use an unwind segue to return to the viewController that called you. For instance, implement this in the first ViewController of the tab (the one calling the modal segue).

@IBAction func backFromModal(_ segue: UIStoryboardSegue) {
    print("and we are back")
    // Switch to the second tab (tabs are numbered 0, 1, 2)
    self.tabBarController?.selectedIndex = 1
}

Then switch to another tab using self.tabBarController?.selectedIndex = n where n is the number of the tab you really want to go to. To set up the unwind segue, you can either control-drag from a button in your modal view controller to the exit icon at the top of the viewController and select backFromModal from the pop up...

drag from button to Exit enter image description here


OR

you can set up the unwind segue to be called programmatically by control-dragging from the viewController icon at the top of the modal viewController to the exit icon, and select backFromModal from the pop up.

enter image description here

Then, go to the Document Outline View and click on the unwind segue

enter image description here

and give it an identifier in the Attributes Inspector on the right (for example "returnFromModal").

enter image description here

Then you'd call the unwind segue like this:

self.performSegue(withIdentifier: "returnFromModal", sender: self)
vacawama
  • 133,454
  • 26
  • 238
  • 261
  • perfect answer! It did exactly with what I wanted to do, I dont know "self.tabBarController?.selectedIndex" existed! – MasterWizard Jun 24 '15 at 08:20
  • How do you pass data to the green view controller? – Suragch Dec 09 '17 at 09:01
  • @Suragch, to pass data to the green view controller from the white one, it is possible to unwind directly to the green view controller and pass the data during `prepare(for:sender:)`. – vacawama Dec 09 '17 at 12:46
  • Oh! I had assumed that I could only unwind directly to a parent. That's great. I'll work on that. – Suragch Dec 09 '17 at 14:12
  • Yeah. Usually it is only a parent, but you can also unwind to any of the base level VC's in a tab view controller. – vacawama Dec 09 '17 at 14:13
  • I followed your advice to segue directly to the second tab and send data. It worked well. [Here is my example.](https://stackoverflow.com/a/47751962/3681880) – Suragch Dec 11 '17 at 11:31
  • Looks good @Suragch. Question. I'm having a mental block. How do you insert links into comments and give them a nice name as you did with "Here is my example" above? Are you manually formatting that, and could you me an example of the syntax for that? – vacawama Dec 11 '17 at 11:54
  • NVM, I found the info [here](https://meta.stackexchange.com/questions/19756/how-do-comments-work/19757#19757). – vacawama Dec 11 '17 at 12:07
  • I wish there were some editor button shortcuts for the comments, but yes, as you discovered I just manually write out `[my text](http://example.com)`. – Suragch Dec 11 '17 at 13:53