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]开发者_如何学JAVA 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.
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"]) ...
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.
精彩评论