Saturday, July 12, 2014

iOS Tutorial - Part 14 - UITextFieldDelegate

UITextFieldDelegate


Video Description 

UITextFieldDelegate

Delegate performs methods on behalf of another object. There are lots of Delegate methods for many different objects. For instance, the Text Field object can't do anything on its own. Instead, it uses a delegate to perform actions. If you press the enter key on the on screen keyboard, the text field asks the delegate object to perform a specific method, textFieldShouldReturn. If the delegate you set for your text field does not have a textFieldShouldReturn method, the text field will not know what to do when you press the enter button.

Tip Calculator with Return button

We can enhance Tip Calculator app in previous session by adding UITextFieldDelegate. We want to add the following options to the application:
1. Make the return key on the keyboard work
2. Dismiss the keyboard once user clicks on the calculate button or the return key on the keyboard

Steps to declare built-in delegates

1- Declare the delegate name

We can declare the delegate name either in .h file or .m file.
Declare in .h file:
@interface ViewController : UIViewController <UITextFieldDelegate>
Declare in .m file:
@interface ViewController () <UITextFieldDelegate>

2- Put the delegate equal to self

By putting delegate equal to self, we tell the compiler that we want to implement the delegate methods. Most of the time, the best place to put the delegate method equal to self is in viewDidLoad (View lifecycle method).
self.totalField.delegate = self;

3- Add the delegate method

The last but not the least is adding the desired delegate method. For example in this tutorial in order to make the return key on keyboard working, we need to use the UITextFieldDelegate. This delegate has a method called: textFieldShouldReturn.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //Add whatever you want to do when the user presses the return key on the keyboard
    return YES;
}

Dismiss keyboard

In order to dismiss the keyboard we need to use a method called resignFirstResponder. It's very easy to use, you just send a message from your textField to this method like bellow:
[self.yourTextFieldName resignFirstResponder]; 

All of the code for this tutorial

//
//  ViewController.m
//  TipCalculatorWithReturn
//
//  Created by Hamid on 7/6/14.
//  Copyright (c) 2014 HuxTek. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
{
    int tipPercentage;
}
@property (weak, nonatomic) IBOutlet UITextField *totalField;
@property (weak, nonatomic) IBOutlet UILabel *tipPercentageLabel;
@property (weak, nonatomic) IBOutlet UISlider *tipPercentageSlider;
@property (weak, nonatomic) IBOutlet UILabel *tipAmountLabel;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

@end

//2. Put delegate = self
//3. Use the delegate methods

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.totalField.delegate = self;
}


- (IBAction)tipPercentageChanged {
    self.tipPercentageLabel.text = [NSString stringWithFormat:@"Tip Percentage %d%%", tipPercentage];
    [self updateResult];
}

- (IBAction)calculatePressed {
    [self updateResult];
}

- (void)updateResult
{
    tipPercentage = self.tipPercentageSlider.value;
    float billAmount = [self.totalField.text floatValue];
    float tipAmount = tipPercentage * billAmount / 100;
    float totalBill = tipAmount + billAmount;
    NSNumber *totalBillWrapper = [NSNumber numberWithFloat:totalBill];
    NSNumber *tipAmountWrapper = [NSNumber numberWithFloat:tipAmount];
    self.tipAmountLabel.text = [NSString stringWithFormat:@"Tip amount: %@", [tipAmountWrapper stringValue]];
    self.resultLabel.text = [NSString stringWithFormat:@"Total: %@",[totalBillWrapper stringValue]];
    [self.totalField resignFirstResponder];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self updateResult];
    return YES;
}

@end

Download

Download the TipCalculatorWithReturn App from here

No comments:

Post a Comment