0

所以我有问题这样做。这段代码不会按我想要的方式工作。

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];
}

我的问题是文本框“答案”中有文本“文本”,它不会执行第一个 UIAlertView。它总是执行 else{} 中的那个。此代码也在按钮的 IBAction 中。现在任何帮助都是好的。

4

2 回答 2

4

==运算符只比较指针,因此两个相同的 NSString 实例不会比较相等。请改用比较方法:

if ([answer.text isEqualToString:@"text"]) ...
于 2011-08-14T05:06:37.110 回答
0
if ([text isEqualToString:@"text"]) {

一个很好的说明:

==在许多语言中,用来比较刺痛通常不是一个好主意。我总是寻找比较字符串函数或方法。我建议你这样做,除非语言手册另有说明。

于 2011-08-14T05:08:33.263 回答