Swift For Dummies
Book image
Explore Book Buy On Amazon

Although building the interface is not strictly speaking a Swift process because Interface Builder builds the code for you, it’s worthwhile taking a look at how you write code for outlets and actions using Xcode and Interface Builder (which is built into it).

In order to let people interact with social media from Locatapp, they need an action button which is typically placed at the right end of a navigation bar, as you will see in the following steps.

Here are the steps to add the action button — and to have Xcode write the code automatically while you’re at it.

  1. Open Main.storyboard in Xcode.

    This launches Interface Builder and displays the canvas for your interface. You may need to zoom the view in or out. Use Editor→Canvas→Zoom.

  2. Make sure you can see MKMapView in DetailViewController.

  3. Open the utilities area, if necessary.

  4. Choose Bar Button Item from the library and drag it to the right of the navigation bar.

  5. In the Attributes inspector, choose Action from the Identifier pop‐up menu.

  6. Choose the Assistant.

  7. Select DetailViewController.swift as the companion view.

  8. Control‐drag from the action button to the top of the DetailViewController class.

  9. When you release the button, use the pop‐up to set this connection to an action rather than an outlet or outlet connection.

    Name it actionButton, and leave the default type as AnyObject.


When you release the mouse button, you’ll see that you’ve created an action like this:

@IBAction func actionButton(sender: AnyObject) {

This technique of control‐dragging from an interface element to a class interface in the Assistant in Xcode is the easiest way to create properties for interface elements such as view or buttons and actions for what happens when an interface element is clicked.

About This Article

This article is from the book:

About the book author:

Jesse Feiler is a developer, consultant, and author specializing in Apple technologies. He is the creator of Minutes Machine for iPad, the meeting management app, and Saranac River Trail and is heard regularly on WAMC Public Radio for the Northeast’s The Roundtable.

This article can be found in the category: