Your First iOS App: 100% Programmatically (Swift)

Part 3

Austin Louden
3 min readMar 27, 2016

This is Part 3 of a tutorial that follows Apple’s “Build a Basic UI”, but implements all of the user interface elements programmatically. View the full project on GitHub. View Part 1 or Part 2.

Now that we’ve got our text field, button, and label, we can get everything working together.

The goal is to change the label text when we hit return on the keyboard, then reset it to our original text when we press the button.

Changing the label text with the keyboard

To do this, we need to implement a UITextField delegate method. Delegates and protocols are an important component of Swift and Objective-C. I’m not going to go into detail here, but you’ll notice that these methods will often give us an opportunity to do something when an event happens.

There’s a lot you can learn from trying it out. At the top of your file, modify the class declaration line to add the UITextFieldDelegate like so:

class ViewController: UIViewController, UITextFieldDelegate {

The is essentially shorthand for: “the name of this class is ViewController, it’s a subclass of UIViewController, and conforms to the UITextFieldDelegate protocol.”

⌘+Click on UITextFieldDelegate once you’ve added it. Here we can see the list of the functions in the UITextFieldDelegate protocol. I’ve included a screenshot of a few.

Notice that all of these functions are prefixed with the word “optional”, meaning a class which conforms to the UITextFieldDelegate protocol can implement these, but does not have to. This is not the case for all protocols.

Next, we need to let our instance UITextField know that our ViewController implements its protocol methods. You can set this in init, next to where you set the other textField property.

textField.delegate = self

Our app needs to implement the “textFieldShouldReturn” method, which is called when a user presses the return key. You can put this below viewDidLoad.

func textFieldShouldReturn(textField: UITextField) -> Bool {
label.text = textField.text
return false
}

When return is pressed, we simply set the text of the label equal to the text of the text field.

Build and run the app. Now, if you type inside the box and hit enter, the label should update!

Resetting the text when pressing the button

Below viewDidLoad, create another function that will be called when the button is pressed.

func buttonPressed() {
label.text = “hello!”
}

Finally, below where you set the textField delegate add this line, which hooks up the button to the “buttonPressed” method you implemented above.

button.addTarget(self, action: #selector(ViewController.buttonPressed), forControlEvents: .TouchUpInside)

Build and run your app. Everything should work as expected — try changing the text of the label by typing in the text field and hitting return, then press the button.

A final word

While this code will work on all iOS devices, writing the layout programmatically means that it will not adapt to them. However — since many companies have chosen to forgo storyboards and interface builder — there are some great alternatives. When you feel ready to make a decision, I’d recommend checking out SnapKit or AsyncDisplayKit.

Feel free to reach out to me with comments or corrections! I’m @austinlouden on Twitter.

--

--