2014-03-26 41 views
0

我有一個故事板,它有一個UITextField,UIButton,UIImageUILabel來顯示數組中的圖像。如果您將圖像文件的正確名稱輸入到文本字段中。所以,問題是,一旦文本字段輸入不是匹配,它應該更新UILabel顯示"Result not found",但它不。當我的if語句錯誤時,我的UILabel不顯示

#import "ViewController.h" 

@interface ViewController() 
{ 
    myClass *myNewClass; 
    NSMutableArray *picArray; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    picArray = [@[@"Button_Red",@"Button_Green"]mutableCopy]; 
} 

- (IBAction)displayImageAction:(id)sender 
{ 
    NSString *titleSearched = self.textSearchField.text; 
    NSString *titleNotHere = self.notFoundLabel.text; 

    //Declare a bool variable here and set 
    BOOL variable1; 

    for (int i = 0; i < picArray.count; i++) 
    { 
     NSString *currentPic = picArray[i]; 

     if ([titleSearched isEqualToString:currentPic]) 
     { 
      variable1 = YES; 
     } 
    } 


    if (variable1 == YES) { 
     //this works fine displays the image 
     self.outputImage.image = [UIImage imageNamed: titleSearched]; 
     [self.textSearchField resignFirstResponder]; 
    } else { 
     //problem is here its not showing when input for the array is not equal it should display a message label "Result Not Found" but it remains blank on the IOS simulator 

     titleNotHere = [NSString stringWithFormat:@"Result Not found"]; 
     [self.textSearchField resignFirstResponder]; 
    } 
} 

//Get rid of the texfield when done typing 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Retract keyboard if up 
    [self.textSearchField resignFirstResponder]; 
} 
+0

什麼是'titleNotHere'?它是設置爲某個或零? – Wain

+0

NSString * titleNotHere = self.notFoundLabel.text;被設置爲這個希望,這有助於 – nateicon

+0

變量1在其聲明行中設置爲FALSE。 – Larme

回答

1

您的問題是

titleNotHere = [NSString stringWithFormat:@"Result Not found"];

只是設置方法變量titleNotHere

你想要的是

[email protected]"Result Not found";

你也想

[email protected]"";

當結果被發現。

+0

它不顯示文本UILabel它只是留下空白。 – nateicon

+0

您是否將UILabel綁定到IB的notFoundLabel屬性?你有沒有在調試器中檢查你的代碼,看看有沒有零? – Paulw11

+0

否我沒有,是的,它返回零,所以我只需將UIlabel連接到IBaction – nateicon

0

此:

NSString *titleNotHere = self.notFoundLabel.text; 

存儲文本從標籤到一個變量,但再次更新該變量不會更改標籤文本 - 它只會改變什麼變量指向。

你需要明確更新標籤文本:

self.notFoundLabel.text = @"Result Not found"; 

還要注意,這裏使用一個字符串 - 你不需要使用的格式字符串作爲您不添加任何參數給它。

另外,在檢查布爾值時,不要使用if (variable1 == YES) {,只需使用if (variable1) {(這樣更安全)。

0

我認爲你必須將值設置爲可變

self.notFoundLabel.text = [NSString stringWithFormat:@"Result Not found"]

self.notFoundLabel setText:[NSString stringWithFormat:@"Result Not found"]

UILabel.text = @"whatever..."實際上轉化爲[UILable setText:@"whatever..."]

NSString *labelText = UILabel.text必須被認爲是NSString *labelText = [UILabel text];