JHHoang
JHHoang

Reputation: 653

This is weird questions about if statement. My If statement doesn't work probably. why?

I have this simple if statement that check UItextfields if empty or not like this:

 if([UserID.text isEqualToString:@""] || [UserPass.text isEqualToString:@""]){
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                               message:@"Fields can not be empty." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertsuccess show];
        [alertsuccess release];
    }

Here is problem, first time when I enter the view has these UItextfields. I test like this

Now the interesting part is here,if I dont exit the view during the test. I check UserID is NOT empty & UserPass is empty again(test#2 above), then the of statement work correctly. If I exit the view during the test, then if statement act weird again.

Can anyone help me to point out what did I do wrong here? Thank you very much

Upvotes: 2

Views: 214

Answers (4)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

Sounds like UserPass (or UserPass.text) is not set to what you think it is; it may be set set to nil, which would make isEqualToString on it to always return false. If you hadn't made it work occasionally, I'd say there's a disconnected outlet.

Upvotes: 1

Peter Warbo
Peter Warbo

Reputation: 11690

Try NSLog("bool value: %d", [UserID.text isEqualToString:@""] || [UserPass.text isEqualToString:@""]); before evaluating the IF-statement to see the Boolean value.

Upvotes: 0

NeverBe
NeverBe

Reputation: 5037

try use : if ([UserID.text length] > 0]) {

Upvotes: 1

Raymond Wang
Raymond Wang

Reputation: 1484

Wired. Did you check whether your UserID and UserPass are defined as IBOutlet and connected in the IB?

Upvotes: 0

Related Questions