In this recipe, we will learn how to use text block methods to determine when editing has begun and ended.
In this recipe, we will discuss the usage of the UITextField
object and how we can display editable text within it.
In order to see how this can be achieved, we need to modify the ViewObjectsExample application that we created in the previous recipe. Perform the following steps to do so:
ViewObjectsExample.xcodeproj
project file.ViewController.xib
file from the project navigator window.UIButton
we added previously.TextField
control width so that it is big enough to hold enough text.TextField
, and name it txtTextInput
.Our next step is to create the code functionality that will be responsible for displaying some text within our TextField
control:
ViewController.m
implementation file from the project navigator.populateTextBox
method, as shown in the following code snippet:-(void)populateTextBox { self.txtTextInput.text = @"This is some sample text"; self.txtTextInput.returnKeyType = UIReturnKeyDone; self.txtTextInput.delegate = self; }
viewDidLoad
method:[self populateTextBox];
Our next step is to modify our ViewController.h
interface file so that we can access the methods for our textbox. This can be achieved by performing the following simple steps:
ViewController.h
interface file from the project navigator.// ViewController.h
// ViewObjectsExample
// Created by Steven F Daniel on 15/09/12.
// Copyright (c) 2012 GenieSoft Studios. All rights reserved.
#import<UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *txtTextInput;
}
@property (weak, nonatomic) IBOutlet UITextField *txtTextInput;
Next, we need to modify our ViewController.m
implementation file to include the method events for our textbox. This can be achieved by performing the following simple steps:
ViewController.m
implementation file from the project navigator.-(BOOL)textFieldShouldReturn:(UITextField *)textField { // Dismisses the keyboard when the "Done" button is clicked [textField resignFirstResponder]; return YES; }
-(void) textFieldDidBeginEditing:(UITextField *)textField { lblInfo.text = @"TextField contents are being updated"; [self.lblInfo setBackgroundColor:[UIColor redColor]]; } -(void)textFieldDidEndEditing:(UITextField *)textField { lblInfo.text = @"TextField contents have now been updated."; [self.lblInfo setBackgroundColor:[UIColor greenColor]]; }
When the compilation completes, the iOS Simulator will appear and display the textbox with our populated sample text, as shown in the following screenshot:
In the following code snippet, we update the text property of the textField
control and then set the returnKeyType
property to dismiss the keyboard when the Done button is clicked and then set the delegate for the control to be our view controller:
-(void)populateTextBox { self.txtTextInput.text = @"This is some sample text"; self.txtTextInput.returnKeyType = UIReturnKeyDone; self.txtTextInput.delegate = self; }
Next, the UITextField
class provides an object that displays editable text. In order for us to respond to the events of our textbox, we need to define a protocol class of UITextBoxDelegate
that will act as the textbox's delegate, so that we can determine when text is being modified.
@interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *txtTextInput;
}
@property (weak, nonatomic) IBOutlet UITextField *txtTextInput;
In the following code snippet, we declare the textFieldShouldReturn:
method of our TextField
control. This method handles the dismissing of the keyboard when the Done button is pressed which is achieved by calling the resignFirstReponder
method on the TextField
control, causing the control to lose focus.
-(BOOL)textFieldShouldReturn:(UITextField *)textField { // Dismisses the keyboard when the "Done" button is clicked [textField resignFirstResponder]; return YES; }
Next, we declare the textFieldDidBeginEditing:
and textFieldDidEndEditing:
methods of our TextField
control. These methods are responsible for determining when text is being updated within the text field and when the editing has been completed, which is achieved when the keyboard is dismissed.
-(void) textFieldDidBeginEditing:(UITextField *)textField { lblInfo.text = @"TextField contents are being updated"; [self.lblInfo setBackgroundColor:[UIColor redColor]]; } -(void)textFieldDidEndEditing:(UITextField *)textField { lblInfo.text = @"TextField contents have now been updated."; [self.lblInfo setBackgroundColor:[UIColor greenColor]]; }
A delegate in Objective-C is a certain type of object that conform to a specific protocol. This means that it is an object that wraps one or more methods (and/or other members) that act as event handlers.