2014-03-26 2 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]"";

를 원할 것입니다

[email protected]"Result Not found";

입니다.

+0

UILabel 텍스트를 표시하지 않으면 그냥 비워 둡니다. – nateicon

+0

IB의 notFoundLabel 속성에 UILabel을 바인딩 했습니까? 아무 것도 없으면 디버거에서 코드를 밟아 보았습니까? – Paulw11

+0

아니요 그렇습니다. 그렇습니다. 반환하지 않으므로 UIlabel을 IBaction에 연결하면됩니다. – nateicon

0

이 :

NSString *titleNotHere = self.notFoundLabel.text; 

저장 변수에 라벨의 텍스트,하지만 레이블 텍스트를 변경하지 않습니다 다시 그 변수를 업데이트 - 그것은 단지 그 무엇 변수 포인트로 변경됩니다.

당신은 명시 적으로 레이블 텍스트 업데이트해야합니다 :이 문자열 리터럴 사용하는

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

메모를 - 당신은 당신이 그것에 어떤 매개 변수를 추가하지 않는 한 형식 문자열을 사용할 필요가 없습니다.

또한 부울을 확인할 때는 을 사용하지 말고 if (variable1) {을 사용하십시오 (더 안전합니다).

0

나는 당신이 실제로 [UILable setText:@"whatever..."]로 변환 변수

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

또는

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

UILabel.text = @"whatever..."

에 값을 설정해야합니다 생각합니다.

NSString *labelText = UILabel.textNSString *labelText = [UILabel text]으로 생각해야한다.

관련 문제