Displaying and editing text

In this recipe, we will learn how to use text block methods to determine when editing has begun and ended.

Getting ready

In this recipe, we will discuss the usage of the UITextField object and how we can display editable text within it.

How to do 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:

  1. Open the ViewObjectsExample.xcodeproj project file.
  2. Select the ViewController.xib file from the project navigator window.
  3. From Object Library, drag a TextField object onto the subview and place it underneath UIButton we added previously.
  4. Resize the TextField control width so that it is big enough to hold enough text.
  5. Next, create the outlet and property for this 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:

  1. Open the ViewController.m implementation file from the project navigator.
  2. Next, create the 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;
    }
  3. Then, add the following line of code within the 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:

  1. Open the ViewController.h interface file from the project navigator.
  2. Next, enter the following code snippet:
    //  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:

  1. Open the ViewController.m implementation file from the project navigator.
  2. Next, type in the following code snippet:
    -(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]];
    }
  3. Then, build and run the application by selecting Product | Run from the Product menu, or alternatively by pressing command + R.

When the compilation completes, the iOS Simulator will appear and display the textbox with our populated sample text, as shown in the following screenshot:

How to do it...

How it works…

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]];
}

There's more…

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.

See also

  • The Using the iOS device keyboard recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset