Building Swift Outlets and Actions with Xcode

By Jesse Feiler

Xcode has Interface Builder built into it so that you can design your interface graphically. The key to making it all work is to connect the graphical elements from Interface Builder to your Swift files. Xcode makes it easy — just follow these steps:

  1. Open Main.storyboard.

    Your main storyboard may have a different name; this is the default name.

  2. Arrange the interface as you want it for the given scene.

    For example, drag a bar button item onto a view. Select Action for its identifier in the Attributes inspector.

    image0.jpg

  3. Switch to the Assistant so that you see the code for your view controller on one side and the Interface Builder design canvas on the other.

    image1.jpg

  4. Control-drag the cursor from the button on the canvas to the declarations at the top of the view controller file.

    image2.jpg

  5. Select Action from the pop-up menu and name the action. (For a property, choose Outlet from the pop-up menu and name the outlet in the same way.)

    Xcode builds the shell of the action function for you to implement.

  6. Click Connect.