Chapter 5. Capturing users’ actions: adding buttons

 

This chapter covers

  • Creating an app with a button
  • Buttons and how to use them
  • Changing labels

You’re going to create another app in this chapter. This one will have a button the user can tap. Buttons are used throughout iPhone and iPad apps to allow the user to do some kind of action, such as make a phone call. Each number on the phone number screen is a button, for example, and the call and hang-up buttons are buttons too.

In this chapter you’ll add a label and a button to the app and write some code. The code you write will make the button change what the label displays. You’re also going to change how the label looks by implementing cosmetic changes.

Looking at the code

You can download all the projects from this book at www.manning.com/books/anyone-can-create-an-app or https://github.com/wlwise/AnyoneCanCreateAnApp, and you can refer to them anytime.

5.1. Adding a label and a button

As usual, we’re going to start with pseudocode steps so that we follow a logical road map for completing this project. Here are the steps:

  1. Start a new project using the Single View Application template.
  2. Add a button and label to the storyboard, and run the app to test it.
  3. Connect the button and the label to the code (wire them up), and run the app to test it.
  4. Add code to change the text on the label when the button is clicked, and run the app to test it.
  5. Change how the label looks, and run the app to test it.

5.2. Changing how the label appears

5.3. Summary