user2995344
user2995344

Reputation: 268

How do I clear my UITextField's text when I tap on it again?

I have a UITextField called numberToAdd and it asks the user to enter a number to be added to 10 and a UIButton that calculates it. Then there's a UILabel that displays the sum.

Say I enter a number 5 into the UITextField and press calculate, the UILabel will show 15.

How do I write a code that will clear the UITextField when I tap on it again instead of having to delete the number 5 manually?

Upvotes: 1

Views: 1455

Answers (3)

Gabriel.Massana
Gabriel.Massana

Reputation: 8225

I can think in three solutions for your problem:

Option 1
Clear the TextField everytime you touch it

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    if (textField == self.numberToAdd) {
        self.numberToAdd1.text = @"";
    }
}

Option 2
Clear the TextField when you touch it, only if you already calculated the number.

@property (nonatomic) BOOL calculateDone;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.calculateDone = false;
}

- (IBAction)calculate:(id)sender {

    //sum label number and textField number
    int sum = [self.label.text intValue] + [self.numberToAdd.text intValue];
    //set label
    self.label.text = [NSString stringWithFormat:@"%d", sum];

    // sum done true
    self.calculateDone = true;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    if (textField == self.numberToAdd)
    {
        if (self.calculateDone)
        {
            self.numberToAdd.text = @"";
            self.calculateDone = false;
        }
    }
}

Option 3
Clear the TextField when you click calculate button

- (IBAction)calculate:(id)sender {

    //sum label number and textField number
    int sum = [self.label.text intValue] + [self.numberToAdd.text intValue];
    //set label
    self.label.text = [NSString stringWithFormat:@"%d", sum];

    //reset textField
    self.numberToAdd.text = @"";

}

You can also check the text entered in the TextField, because you only wants numbers in the TextField, right?.

@property (strong, nonatomic) NSString *urlRegEx;
@property (strong, nonatomic) NSPredicate *urlRegExTest;

//This Regex accepts negative and positive numbers
self.urlRegEx = @"((-)?[0-9]*)";
self.urlRegExTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", self.urlRegEx];


- (IBAction)calculate:(id)sender {

    // if text is a valid Number
    if ([self.urlRegExTest evaluateWithObject:self.numberToAdd.text]) {

        int sum = [self.label.text intValue] + [self.numberToAdd.text intValue];
        self.label.text = [NSString stringWithFormat:@"%d", sum];
    }

}

You can download an example code here.

Upvotes: 0

Son Nguyen
Son Nguyen

Reputation: 3491

Firstly you should have a flag which indicate number is calculated or not, after user tapping calculate button, you should turn on that flag, then when user taps the TextField again, you can use UITextFieldDelegate detect the action and clear the textfield as below answer:

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    if (isCalculated) {  
        textField.text = @""; 
        isCalculated = NO;  
    }  
}

Upvotes: 2

iBhavin
iBhavin

Reputation: 1261

You can use UITapGestureRecognizer to get clear text on TextField.First you should use BOOL flag for know if it calculated or not let say BOOL flag is isCal.

 UITapGestureRecognizer *clearTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearTextField:)];

 [self.textView addGestureRecognizer:clearTapRecognizer];

and in clearTextField method write belove code.

- (void)clearTextField:(UITapGestureRecognizer *)gestureRecognizer
{
     if(isCal)
     {
        UITextView *_textView = (UITextView*) gestureRecognizer.view;
        _textView.text=@"";
        isCal=NO;
     }
}

Upvotes: 0

Related Questions