tags:

views:

72

answers:

3

I have a simple calculator that works, but my problem is if the user doesn't input any data into the fields then it still calculates. I thought I could do the following:

if (txtUserName.text == nil || txtUserName2.text == nil || txtUserName3.text == nil || txtUserName4.text == nil || txtUserName5.text == nil)
    {
        //error message 
        lblUserTypedName.text =@"Error - No Field Input";
    }
    else
    {
    lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%%", ultimate_risk];
    }

But it's not working at all. Any help letting me know what I am missing here would be greatly appreciated. I would like to use the || so even if they input all fields except one this will still catch it.

+4  A: 

you're going to want to do something like [txtUserName.text isEqualToString:@""] because an empty string is not the same as a nil object. You also might want to strip whitespace from the strings before checking if they are empty, depending on what your app does. I would put whitespace stripping in the UITextField editing delegate methods, rather than in this method.

EDIT:

if ([txtUserName.text isEqualToString:@""] || [txtUserName2.text isEqualToString:@""] || [txtUserName3.text isEqualToString:@""] || [txtUserName4.text isEqualToString:@""] || [txtUserName5.text isEqualToString:@""])
{
  //error message 
  lblUserTypedName.text =@"Error - No Field Input";
}
else
{
  lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%%", ultimate_risk];
}
Kenny Winker
but how would i be able to put this into an if/else? becuz if the user has input data i want it to calculate, if they haven't i don't want it to calculate.
HollerTrain
Just replace `txtUserName.text == nil` with `[txtUserName.text isEqualToString:@""]`.
kiamlaluno
added full example code to my answer. bodnarbm's string length technique works as well.
Kenny Winker
hmm. i thought i was using same code, but it's giving me an error. What am i doing wrong here? (http://screencast.com/t/YjU1NGRhMTQ)
HollerTrain
HollerTrain, I think it's stopping because you have a breakpoint set right before the line... either remove the breakpoint (blue tab on the left side of the editor window) or run with breakpoints off (command-option-r)
Kenny Winker
ah yes you are totally correct. TY!
HollerTrain
+1  A: 

I would suggest using the length method of NSString to determine if there is any input. so you could use

if ([txtUserName.text length]== 0 || [txtUserName2.text length]== 0 || [txtUserName3.text length]== 0 || [txtUserName4.text length]== 0 || [txtUserName5.text length]== 0)
{
    //error message 
    lblUserTypedName.text =@"Error - No Field Input";
}
else
{
lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%%", ultimate_risk];
}

This will work for both the text property being nil (which is the default value according to the documentation) and the text property being the empty string.

Also note that this still performs the calculation, it just does not display it. If you want to prevent the calculation you need to move the code to calculate ultimate_risk into the else block.

Brandon Bodnár
when i try to use this code it does bring up the simulator, but does stop directly at the if statement you used above. Thoughts? (http://screencast.com/t/NDJjYjE3)
HollerTrain
thanks this works perfectly. this is a much better solution
HollerTrain
A: 

If you want to check for more than one string you can do this

if([txtUserName.text isEqualToString:@""] ||
 [txtUserName2.text isEqualToString:@""] ||
 [txtUserName3.text isEqualToString:@""] ||
 [txtUserName4.text isEqualToString:@""] ||
 [txtUserName5.text isEqualToString:@""] 
 )
{
 //error message 
  lblUserTypedName.text =@"Error - No Field Input";
}
else
{
lblUserTypedName.text = [[NSString alloc] initWithFormat: @"%2.1f%%", ultimate_risk];
}
Mez