Beginning iOS Development

Course No. 10-152-143

Text Input — A Gentler Introduction

  • Create a new iOS Single View Application. Name it TextMe.
  • Use your own 3-character prefix. I’ll be using my initials in the screenshots.
  • Open the ViewController.xib file for editing.
  • Uncheck the Use Autolayout checkbox in the Identity Editor, that’s the first tab.
  • Change the background color to something other than the default. I went with boring white.
  • Add a text field and a label to the view. If you need to, change the background color of the label to make it visible.


  • Option-click on the ViewController.m file and wire the label to an outlet property.


  • Control-click on the text field and wire the delegate to the File’s Owner.


  • Add the UITextFieldDelegate protocol to the ViewController.h file.
  • Here are the methods in the UITextFieldDelegate protocol.
  • Tasks
  • Managing Editing
    • – textFieldShouldBeginEditing:
    • – textFieldDidBeginEditing:
    • – textFieldShouldEndEditing:
    • – textFieldDidEndEditing:
  • Editing the Text Field’s Text
    • – textField:shouldChangeCharactersInRange:replacementString:
    • – textFieldShouldClear:
    • – textFieldShouldReturn:
  • These are the methods you need to implement to use text fields. They are all optional and the one we use the most is textFieldShouldReturn:
  • Implement the method like this.
  • Let’s look at that method line by line.
    • Get the text that was typed into the text field and set the label’s text.
    • Clear our the text field.
    • Stop being where input can happen. Like losing focus.
    • Make the keyboard go away!
  • Run the app.


  • Now tap the text field, enter some text.


  • When you tap the return key the text will appear in the label, the text field will be empty, and the keyboard will go away.


  • Woo!