简体   繁体   中英

How to check if a UITextfield has Text in it?

I just followed a tut on making a conversion app. It was good, but I wanted to expand on it. The tut has you input a value in for Fahrenheit and then converts to Celsius. Pretty basic. So I wanted to add a Kelvin conversion as well. But the code only let you plug in a number for the Fahrenheit. So after adding the Kelvin text field, I wanted to check to see which text box had text in it. So I used the following code:

- (IBAction)convert:(id)sender 
{
if ([fahrenheit isFirstResponder]) 
{
    float x = [[fahrenheit text] floatValue];
    float y = (x - 32.0f) * (5.0f/9.0f);  //celcius
    float z = y + 273.15f;  //kelvin
    [celcius setText:[NSString stringWithFormat:@"%3.2f" , y]];
    [kelvin setText:[NSString stringWithFormat:@"%3.2f" , z]];
    [fahrenheit resignFirstResponder];
} else if ([celcius isFirstResponder])
{
    float x = [[celcius text] floatValue];
    float y = 32.0f + ((9.0f/5.0f) * x); //farenheit
    float z = x + 273.12f; //kelvin
    [fahrenheit setText:[NSString stringWithFormat:@"%3.2f" , y]];
    [kelvin setText:[NSString stringWithFormat:@"%3.2f" , z]];
    [celcius resignFirstResponder];
}else if ([kelvin isFirstResponder])
{
    float x = [[kelvin text] floatValue];
    float y = x - 273.15f; //celcius
    float z = 32.0f + ((9.0f/5.0f) * y); //farenheit
    [celcius setText:[NSString stringWithFormat:@"%3.2f" , y]];
    [fahrenheit setText:[NSString stringWithFormat:@"%3.2f" , z]];
    [kelvin resignFirstResponder];
}
}

This allowed me to input a number in any text field and then convert. But then I decided to dismiss the keyboard. My code said to resignFirstResponder. But then the convert action did not work because now there was no first responder. Any clues as to how I can check which text box has text in it, and then do the conversions? Thanks in advance for any help.

    if( [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] != nil &&  [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] != @"" )
        {

        // text field has text


// get text without white space

NSString * textWithoutWhiteSpace = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ;



        }

This is for checking textView is empty or not:-

if([textView.text isEqualToString:@""])
{
    //textView is Empty
}
else 
{
   //textView has text
}

If you want to check it for white space as well, first remove white spaces from string then check ... like this -

NSString *trimmedString = [tV.text stringByTrimmingCharactersInSet:
                           [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([trimmedString isEqualToString:@""])
{
       NSLog(@"textView is empty");
}
else
{
    NSLog(@"textView has some value");
}

Just use the hasText method.

Example:

if(_yourTextField.hasText)
{
    // Do something.
}
if(textView.text.length > 0)
{
   //text present
}
else
{
  //no text
}

Better solution is make all conversions on the fly, add new action to all textFields

[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];

Then in method textChanged: do something like this:

- (void) textChanged:(UITextField *)tf {
    if (tf.text.floatValue > 0) {
        if (tf == fahrenheit) {
            //Do convertion for fahrenheit
        }
        .
        .
        .
        //etc.
    }
}

On response to Meno's answer

DO NOT USE != @""

this check for pointer equality vs String equality

use:

[string isEqualToString:@""];

如果您想在输入期间知道它,并且可能根据此信息执行操作,您应使用:

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

There were a few problems in some of the other answers, like they didn't use isEqualToString, and they superfluously removed potential characters from a string that we are only interested in if it is nil or not.

I don't have enough reputation to comment, so I am posting this as an answer.

For a similar issue, I used this to check each textfield that I needed to check for being empty:

- (BOOL) isEmpty:(UITextField*) field
{
    if (!(field.text) ||
       ([[field.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString: @""]))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

If you need an NSString with white space removed:

NSString *nonWhiteSpaceString = [textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Then you could use the length as a boolean:

BOOL textFieldHasText = nonWhiteSpaceString.length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM