casillas
casillas

Reputation: 16813

How to know textFields are fulfilled

I would like to create a simple calculator application with slightly different interface design. As shown in the below image, I have three inputs from user: first number, operator and second number. My question is that once user fullfills these three textfields, then equal sign(resultBtn) changes color. I would like to know how to know when all these textfields are full. By the way, I have just updated Xcode from 5.1 to 6.0.1 then once I click on textfield, keyboard does not show up.

- (IBAction)resultBtn:(id)sender {

    double temp;

    if([operatorTF.text isEqual:@"+"])
    {
    temp=[calculate arithmetic:[firstNumberTF.text doubleValue] enterCharacter:Plus enterSecond:[secondNumberTF.text doubleValue]];
    }
    else if([operatorTF.text isEqual:@"-"])
    {
    temp=[calculate arithmetic:[firstNumberTF.text doubleValue] enterCharacter:Minus enterSecond:[secondNumberTF.text doubleValue]];
    }
    else if([operatorTF.text isEqual:@"/"])
    {
      temp=  [calculate arithmetic:[firstNumberTF.text doubleValue] enterCharacter:Divide enterSecond:[secondNumberTF.text doubleValue]];
    }
    else if([operatorTF.text isEqual:@"^"])
    {
        temp=  [calculate arithmetic:[firstNumberTF.text doubleValue] enterCharacter:Power enterSecond:[secondNumberTF.text doubleValue]];
    }
    else {
       temp= [calculate arithmetic:[firstNumberTF.text doubleValue] enterCharacter:Multiply enterSecond:[secondNumberTF.text doubleValue]];
    }
    resultTF.hidden= NO;
    resultTF.text=[NSString stringWithFormat:@"%1.2f",temp];
}

- (IBAction)clearBtn:(id)sender {
    firstNumberTF.text = nil;
    resultTF.text = nil;
    secondNumberTF.text = nil;
    operatorTF.text=nil;
    resultTF.hidden= YES;
   // [resultBtnOutlet setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

}

- (BOOL)textFieldShouldReturn: (UITextField *)textField {

    if(textField.tag == 0)
    {
        firstNumberTF.text = textField.text;
    }
    else if(textField.tag == 1)
    {
        operatorTF.text = textField.text;
    }
    else if(textField.tag == 2)
    {
        secondNumberTF.text = textField.text;
    }
    else
    {
        resultTF.text = textField.text;
    }

    [textField resignFirstResponder];
    return YES;
}

enter image description here

Upvotes: 0

Views: 78

Answers (2)

Dheeraj Kumar
Dheeraj Kumar

Reputation: 500

when you writing in the uitextfield the delegate method

  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

will call check in this method the length of all uitextfields is greater then 0, means all the text fields are fullfill.

firstTxt.lenght>0

Upvotes: 1

cubecubed
cubecubed

Reputation: 238

Use if (![firstNumberTF.text isEqualToString:@""]) to check if txtField is empty.

Use the following to check multiple text fields at the same time.

if (![firstNumberTF.text isEqualToString:@""] && ![operatorTF.text isEqualToString:@""] && ![secondNumberTF.text isEqualToString:@""]) {
   resultBtnOutlet setTitleColor:[UIColor grayColor] forState:UIControlStateNormal;
}

Upvotes: 0

Related Questions