Thursday, April 01, 2010

Modifying default UITextField behaviour - Iphone development

1. Create a cancel button in UITextField.
By default, the cancel button is disable when a new UITextField control is added to view. The button can be enabled by changing the option for Clear Button of the UITextField property.

Eg.



2. Disable the clearing old text in UITextField editing.
By default, when text is input to the UITextField, any previous entered text will be cleared. This can be disabled by unchecking the clear when editing begins option.
Eg.

Dismiss IPhone keyboard in text field control

By default, IPhone keyboard will not dismiss automatically after user click on the done button or touch other control or view. IPhone developer has to code this behaviour into the control.

The IPhone keyboard will only be dismissed after a resignFirstResponder message is send to the control.
Eg.
                 [textField resignFirstResponder];

There are probably many ways to add this message to the viewController code.  Below show two of them:

1. Implement  the textFieldShouldReturn delegate for the UITextField. Iphone will pass a message to textFieldShouldReturn method when the keyboard's done button is pressed. Call the send the resignFirstResponder message to the active UITextField will dismiss the keyboard. The following section outline the steps to accomplish this:

a)  Add to the viewController.h interface file to specify that it will implement the delegate textFieldShouldReturn method.
Eg.
@interface sampleViewController : UIViewController  
                                                    <UITextFieldDelegate>     
  // add this line to implement textFieldShouldReturn method, when more than one use comma

b) Implement the textFieldShouldReturn method in the .m implement file.
Eg.
-(BOOL)textFieldShouldReturn:(UITextField *)thisTextField  {
    [thisTextField resignFirstResponder];
    // add code to process the input if needed
    return TRUE;
}

c) link the textField to the class that implement the method.
Eg.


2. Override the touchesBegan method. Iphone will send the touchesBegan message when the view is being touched. In the overrided touchesBegan method, send the resignFirstResponder message to the active UITextField will dismiss the keyboard.
Example add the following code to the viewController:
- (void)touchesBegan:(NSSet *)touches withEvent: (UIEvent *)event {
    if( textField.editing) {   // UITextField.editing will return true if it is in the editing mode

       [textField resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];     // call the default touchesBegan method
}

Create a new control - IPhone development

1. Create control in view using Interface Builder's Liberary. As shown below:

2. Create outlet in view controller code.
  • In the .h file, define the outlet for the control, example:
             IBOutlet UITextField *textField;   // add this to the @interface section
             @property (nonautomic,retain) UITextField *textField;   // add this outside of the @interface section

  • In the .m code, example:
             @synthesize textField;    //add this into the .m code after the @implement line

3. Link the control in Interface Builder view, to the defined Outlet variable. Example:
4. Now the control, can be used in the code.
    Eg:
                 textField.text = @"hello world";     // to assign hello world to the text field

5. As the textField variable is defined as retain in the .h file, it need to be manually released in the dealloc
    method.
    Eg:
- (void) dealloc  {
   [textField release];   // add this line to manually relase the textField variable
   [super dealloc];
}