3

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.

Douglas
  • 2,524
  • 3
  • 29
  • 44
  • Why exactly are you trying to resignFirstResponder after each "if"? Wouldnt the user hit the "dismiss keyboard" or "enter" buttons on the keyboard when done entering numbers? – MrHappyAsthma Jul 25 '12 at 12:50
  • @MrHappyAsthma, I guess I was setting up for the next time they input something. Since I was relying on that to check if the text box was filled. – Douglas Jul 25 '12 at 13:00
  • Ohhh... If that is the case I would look at something like Moonkid's answer. Using delegates is probably your best bet. But this may work too as I've never tried to handle it this way. – MrHappyAsthma Jul 25 '12 at 13:02

9 Answers9

9
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]];

}
Joel
  • 15,654
  • 5
  • 37
  • 60
Ayman Melhem
  • 493
  • 3
  • 13
7

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");
}
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • 1
    Thanks for the input. I will definitely try both of these answers. Wish I could vote for two answers!!! – Douglas Jul 25 '12 at 12:50
  • what if it contains white space? – Paresh Navadiya Jul 25 '12 at 12:52
  • @Prince, this would just check if it is a completely empty NSString. He would have to handle white space differently/separately. – MrHappyAsthma Jul 25 '12 at 12:53
  • @Prince - He asked for to check whether the textView contains text or not now he has changed..... if you are asking me for white space then see his question he need to filter alphabets as well... – TheTiger Jul 25 '12 at 14:10
5

Just use the hasText method.

Example:

if(_yourTextField.hasText)
{
    // Do something.
}
Community
  • 1
  • 1
Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
1
if(textView.text.length > 0)
{
   //text present
}
else
{
  //no text
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
1

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.
    }
}
Moonkid
  • 881
  • 7
  • 17
1

On response to Meno's answer

DO NOT USE != @""

this check for pointer equality vs String equality

use:

[string isEqualToString:@""];
Heavy_Bullets
  • 518
  • 1
  • 4
  • 13
0

If you want to know it DURING input, and probably performs actions based on this info, you shall use:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Vincent
  • 4,342
  • 1
  • 38
  • 37
0

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;
    }
}
narco
  • 830
  • 8
  • 21
0

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;
Tom Howard
  • 4,672
  • 2
  • 43
  • 48