How to Create Outlets and Actions in Xcode Using Swift - Swift Tips 6

Xcode with the Assistant Editor (Venn Diagram button) let's you connect code and UI (user interface) files together.

In order for you to make your UI interactive, you need to create connections from the UI to your related code files. These connections give Xcode names that you can use in your code file to modify how something looks programmatically or to respond to user input (button tabs, sliders, switches, etc). There are two primary connections you will create in Xcode: outlets and actions.


Outlets enable you to give a UI element a name that you can use in your code. You can create a connection from a label to your code file using an outlet.

Outlets will look like:

@IBOutlet weak var textLabel: UILabel!

You can modify properties associated with your UI element, like text and textColor for UILabel objects.


Actions let you trigger things that can happen in response to user input. When a user moves a slider, enters a date, or types their name you can run a chunk of code to respond to it.

Actions will look like:

@IBAction func buttonPressed(sender: AnyObject) {

    print("hi developer")

    textLabel.text = "you pressed me "

Creating Outlets and Actions

  1. Open the Assistant Editor
  2. Right-click and drag from your UI element (i.e. label) to the code file
  3. Xcode auto inserts code for you to create the name and connection

Variables and Properties

You can also create variables and properties in your Swift code files. For ViewController.swift files it is easier to initialize variables and properties when you declare them, or in your viewDidLoad() method.

Swift Tips Source Code

Grab the source code for this Swift Tip and many more at: