Browse By

iPhone: Create Displaying Text Example in iPhone App

In the last article, we have seen the Introduction of Xcode 7.0 and also, we have created the first app. Today we are going to create Displaying Text from a UITextField within our application into a label.iPhone: Create Displaying Text Example in iPhone App

The UIKit framework provides three primary classes for Displaying Text content in an app’s user interface:

  •  UILabel:
    UILabel displays a static text string.
  •  UITextField: 
    UITextField displays a single line of editable text.
  •  UITextView:
    UITextView displays multiple lines of editable text.

Text Field displays editable text and sends the action message to a target object. This allows the users to import their own data into the application and get displayed within the label.

You probably would have seen this in forms or like games where you need to put your username.

iPhone: Create Displaying Text Example in iPhone App

To do this, simply perform the steps given below.

1. Find UITextField, UIButton and a UILabel from Object Library and drag and drop it nicely centered into the view controller.

2. Bring Assistant editor by pressing the button located at the upper right corner. Make sure to set it within ViewController.h

Now we are going to create Action and Outlets of objects.

3.The first outlet we are going to need is of the UITextField. So drag UITextField by Ctrl + click the ViewController.h file. Give name like “textField” to it and connect.


4. Perform same as step 3 for label to create our second outlet for UILabel named “label”

5. Now we are going to create the action for our UIButton. Drag it same as an outlet and give a name like “textFieldPressed”. Before pressing connect make sure the connection type is set Action and event set to Touch Up Inside.


So, now we can move on to give instruction to our action button. What we want to do is take the information from our UITextField and then press the UIButton and display that information within the UILabel.

One of the brilliant things about Xcode is there is no kind of configuration needed to import or use the UITextField. The objects already know what to do when the user selects the UITextField and how they need to react.

6. Open ViewController.m file where a textFieldPressed method is automatically created. Type “self.label.text = self.textField.text;” between braces {} of our action field. Here “textField” and “label” are outlet names were given to text field and label and respectively.


What we do here is bring text attribute of UILabel and equal it to the text attribute of UITextField to make the value of label to the value given by the user in a text field.

Now you can build and run your project.

If you want to bring up the keyboard automatically when you select the text field, perform an operation.

Simulator -> Hardware -> Keyboard -> Check toggle software keyboard

When entering text in a text field, the keyboard loads up but we can’t get rid of it by pressing return.

To dismiss the keyboard we are going to create an action. To do this, we will gain action of the button, which we already have. Perform following instructions.

A. Disconnect connection of UITextField and the UIButton from ViewController.h file and delete the UIButton by pressing (x) button. The connection is shown in the received action field in the connection inspector of the ViewController.


B. Create the action onto our UITextField. Here selectDidEndOnExit action type named textFieldPressed.

For better interface select attribute inspector of the text field and change return key to Done from Return. It will create a Done button.

When you run the project you can see that we have a done button and when you press it, it will perform the action in terms of dismissing the keyboard and display the text written in the text field.

For some older iOS devices, it doesn’t work like this. For that, we need to add action manually to dismiss the keyboard.

Place the code “[self resignFirstResponder];” in the action that we added for the UITextField after the display text code in the ViewController.m file.

What this does is resign the first responder on the screen. In our case the first responder is the keyboard.

It’s good to add that piece of code hard coded anyway if you are working with older devices or not.

Now Execute Application


I hope you like this article. Share your views to improve content. Please stay tune with us. In next article, we will learn how to Customize the Text of UILabel Programmatically.

Subscribe for more articles!!