This video will show you how to show a new view from a tab in a tabbed application and properly dismiss the new view.

Here is the full source code for the example I provided above: Tabbed View Segue Example

Screen Shot 2014-10-11 at 2.47.37 PM

Start by adding a button to one of the tab views.  I added a button to my First View.

Screen Shot 2014-10-11 at 2.54.38 PM

Next, drag a view controller onto the storyboard.  Add a label to it just so you know that the correct view actually loaded.  Then add a button to the upper right corner of the view and give it the text Done.  This Done button is going to be used to dismiss the new view and return to the original tab view that launched the new view.

Now we need to add a swift view controller code file to the project so that we can wire up the Done button and have the view controller dismiss when we click that button.

Just add a new swift file to the project.  I called mine MySecondViewController (sorry I chose a poor name for this view controller when I was recording the demo).

segue_swift_file

 

Add the boilerplate code and also add the new DoneAction function.

Now that you have this new view controller class we need to assign it to the new view that we created.  You do this by clicking on the yellow View Controller button on the view and then choosing your new view controller class in the Custom Class drop down.

link view controller to custom view controller code

Now we need to connect the View 2 button from the first view tab view to the new view.  You do this by Control clicking on the button and dragging the mouse over to to the new view.  As you see in the screenshot below you will see a blue line.  

CreateSegue

When you let go of the mouse button you will get a Segue menu.

SegueMenu

 

On the Segue menu you click the show option.  This will create a Segue that shows the view that you connected the button to.

wire up button to ibaction

You now have to wire up the Done button to the @IBAction we created earlier so that clicking that done button will actually dismiss the view.  The easiest way to do this is to click the assistant view (see arrow in the above screenshot) and then control drag the button to the IBAction and let go.  This links the button to the code in the custom view controller.

Run your application now and you will see that clicking the button on the first view will segue to the new view and clicking the Done button will properly dismiss the new view.

At the end of the video I show you how to use Instruments in order to see that by dismissing the view controller memory is freed up.  If you were to create a segue from the new view back to the first tab you would create a memory leak because you would be infinitely creating new views and displaying them.