Search code examples
iphonetexttextboxalertview

Xcode: If Statements with textbox


So im having problems do this. This code wont work the way i want it to.

if (answer.text == @"text") {
    UIAlertView  *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}else{
    UIAlertView  *alert1 = [[UIAlertView alloc] initWithTitle:@"title 2" message:@"Title 2" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert1 show];
    [alert1 release];
}

My problem is the textbox 'answer' has the text 'text' in it, it wont do the first UIAlertView. It always does the one in the else{}. This code is also in an IBAction for a button. Any help now is good.


Solution

  • The == operator only compares pointers, so two identical instances of NSString won't compare equal. Use a comparison method instead:

    if ([answer.text isEqualToString:@"text"]) ...