iOS Basic UI Elements Example Tutorial

In this tutorial we’re going to discuss and implement the basic UI elements such as text fields, labels and buttons.

Overview

Labels as we had seen in the first tutorial itself, are used for displaying static content.
TextFields are UI elements that enable user input.
Buttons are used for handling user actions.
Buttons and Labels we had implemented at a basic level in the first tutorial. Let’s look into UITextField.

iOS UITextField

UITextField supports the use of a delegate object to handle editing-related event handling. When certain actions occur, the UITextField will call delegate methods that allow its behaviour to be customised and controlled. For this to occur we need to add the UITextFieldDelegate Protocol.

Some delegate methods include :

  1. -(void)textFieldDidBeginEditing:(UITextField *)textField
  2. -(void)textFieldDidEndEditing:(UITextField *)textField

We can customise the UITextField from the attributes inspector as shown in the image below.

ios-basic-ui-image-attributes

The Placeholder attribute needs the hint text as the input.
Clear button attribute is set to Appears while editing. On clicking the button on the right of the textfield it deletes all the characters from the TexField. The Keyboard type can be chosen from the numerous options available in the dropdown. These attributes can also be set programmatically. We’ll add a UITextField programmatically too.

Implementation

We’ll add a Label, Button and TextField onto the ViewController. Drag the TextField to the ViewController dock file’s owner button. And choose delegate. Open up the assistant editor and drag the three outlets onto the header file and name them accordingly. Your screen should look like the one given below.

ios-basic-ui-image-1

The Main.storyboard ViewController should look like the one given below

ios-basic-ui-image-2

The red lines you see are the margins between the UI elements on the screen. They’re set by holding control button and dragging the line from one UI element to the other. We can then chose from the list of options.

The ViewController.h code is given below:

We’ve defined a custom method that would be implemented later to handle the button action. Also we’ve added a property for a UITextField that would be added programmatically.

Building and run the project at this point. You’ll see that you’re able to write in the textfields but you’ll be unable to dismiss the keyboards since the return key and the tap outside to dismiss are not configured.

If the return key is clicked the protocol will call textFieldShouldReturn: delegate method in our class. We’ll dismiss the keyboard by calling the resignFirstResponder method on the textfield object.

The ViewController.m is defined below.

To dismiss the keyboard when tapped outside, the following code snippet is used.

When we’re typing in a text field, the isEditing property of the text field is set to YES. The endEditing: message is passed to a view and all of its subviews. If we pass YES, it forces any and all view objects to call resignFirstResponder, in turn dismissing the keyboard.

A TextField is added programmatically using [self addTextFieldWithDifferentKeyboard]
CGRectMake(topX, topY, width, height) is used to define the layout. The other attributes are similar to the one chosen from the attributes inspector. We’ve used a Number pad input type keyboard. There are many input types of the TextFields such as :

  1. UIKeyboardTypeASCIICapable
  2. UIKeyboardTypeNumbersAndPunctuation
  3. UIKeyboardTypeURL
  4. UIKeyboardTypeNumberPad
  5. UIKeyboardTypePhonePad
  6. UIKeyboardTypeNamePhonePad
  7. UIKeyboardTypeEmailAddress
  8. UIKeyboardTypeDecimalPad
  9. UIKeyboardTypeTwitter

Note: The simulator keyboard may not pop up in some cases. This is due to an an already present hardware keyboard. Go to Simulator->Hardware-> Uncheck hardware keyboard. It would work fine then

The button action is defined to change the label text to the TextField that was defined in the ViewController.

The output of the application in action is shown below.
ios-basic-ui-output

This brings an end to this tutorial. You can download the BasicUI XCode Project from the link given below.

By admin

Leave a Reply

%d bloggers like this: