0

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.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Tyler
  • 3
  • 3
  • 1
    try comparing strings like this: `[answer.text isEqualToString:@"text"]` Also make sure you have outlets correctly connected (`answer` is `IBOutlet`). – Eimantas Aug 14 '11 at 05:06

2 Answers2

4

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"]) ...
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0
if ([text isEqualToString:@"text"]) {

One good note:

In a lot of languages it usually isn't a good idea to use == to compare stings. I always look for a compare string function or method. I would advise you do, unless the language manuals say otherwise.

Dair
  • 15,910
  • 9
  • 62
  • 107