Skip to content

Segues

Prepared and tested with Xcode 10.2.1 and Swift 5 (2019-05-03)

In this tutorial we cover the following topics


General information

In this tutorial you can find some most frequently needed information about segues. Some of them are presented also in my other tutorials, for example in Multiview application (Swift). Here I try to collect all of them and extract so we can focus only on segues themselves but not other details not directly related with segues.


Project

  • Step 1: create a new project
    Create a new project

    1. Select File | New | Project...
    2. Select Single View Application on the template selection sheet.
    3. As the product name enter Swift iOS Segue
    4. Set the Language to Swift. Because we will not use tests in this tutorial, you can leave both test check boxes unchecked as well as make sure the check box labeled Use Core Data is also unchecked.
  • Step 2: add all UI components
    Create a new project

    1. In the Project navigator select Main.storyboard file.
    2. Press Command + Shift + L to open library with UI components and drag a second View Controller and drop it in the editing area next to the existing one
    3. Select from the library a Vertical Stack View component and drag it to the first View Controller
    4. Select from the library one Text Field and two Buttons components and drag all of them to the Vertical Stack View we have just added in previous step
    5. Set all the necessary constraints: Leading Space, Top Space and Trailing Space
  • Step 3: create segue
    1. To Create segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller (in our case this is a first button), drag to the target view controller and select Show from popup menu.
  • Step 4: rename buttons and set name for segue
    1. In Attributes inspector tab change name for a first button to Go with segue

      and for a second button to Go without segue
    2. Clik on the segue (arrow connecting controllers) and set its Identifier to SegueFromFirstToSecond

  • Step 5: segue code in ViewController controller
    1. Select a first view controller either by clicking yellow circle on the top of View Controller (this is showned on the image below)

      or by selecting this View Controller in the Document Outline (hidden in the image above).
    2. Clik on the Show the Assistant editor

      to display this controller class' source code located in ViewController.swift file
    3. Create an action method connected to Go without seguebutton. Control-click, drag from this button to ViewController class and name it pressButtonGoWithoutSegue
    4. Create an textField outlet for a text field. Control-click and drag from a text field to ViewController class
    5. In the Project Navigator, right-click the Swift iOS Segue group and select New File....
    6. Choose Cocoa Touch Class from the iOS / Source section in the template dialog and then press Next
    7. Name the new class VC_Second, make it a subclass of UIViewController. Make sure that the Also create XIB file check box is not checked.
    8. Select a second view controller either by clicking yellow circle on the top of View Controller or by selecting this View Controller in the Document Outline. In the Custom Class section of Identity inspector tab on the right, change Class proprty to VC_Second
    9. Add (or modify) the following code in the VC_Second.swift

    10. Add (or modify) the following code in the ViewController.swift

    11. Run emulator, leave a text field empty

      and press the Go with segue button

      The message

      should be displayed.

    12. Again leave a text field empty

      but this time press the Go without segue button

      This time the message

      should be displayed and the second view should be presented.

    13. Fill a text field with test text

      and press the Go with segue button

      The message

      should be displayed and the second view should be presented.

      You may notice that calling performSeguewithIdentifier doesn't call shouldperformseguewithIdentifier. This behaviour is perfectly natural, for the following reasons:

      • shouldPerformSegueWithIdentifier is used to make sure that a segue that has been set up in storyboard should be triggered, so it only gets called in the case of storyboard segue and gives you the chance to not actually perform the segue.
      • When you call performSegueWithIdentifier yourself, shouldPerformSegueWithIdentifier is not called because it can be assumed that you know what you are doing. There would be no point in calling performSegueWithIdentifier but then return a false from shouldPerformSegueWithIdentifier.
  • Step 6: create an unwind segue
    1. To the second view controller add a button, place it in the middle of a view and name it Go back
    2. In the view controller you are trying to go back to, ViewController in our case, add this code:
    3. In the storyboard, go to the view you’re trying to unwind from, VC_Second in our case, Control-click the Go back button and drag it to the Exit icon located on top.
    4. From the popup menu, select the unwind segue action you just created in ViewController (myUnwindAction in our case)
    5. If you want to trigger unwind manually, drag the view controller yellow circle icon located on top of the view controller on the left to the Exit icon (also located on top but on the right). In this case you have to add also code similar to this

      where someAction is a name of an action (in this case trigerred by a button press), and unwindIdentifier is an identifier of an unwind segue (remember to add this identifier to Identifier property of Storyboard Unwind Segue section in Attributes inspector.
    6. Now you can test if Go back button works (it should).