我有一个故事板,其中有一个UITextFieldUIButtonUIImageUILabel可以显示数组中的图像。如果在文本字段中输入图像文件的正确名称。因此,问题在于,一旦文本字段输入不匹配,它就应该更新UILabel以显示"Result not found",但是不会。

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

最佳答案

你的问题是

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

只需设置方法变量titleNotHere

你想要的是

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

你也会想要

self.notFoundLabel.text=@"";

找到结果时。

关于ios - 我的if语句为false时,我的UILabel没有显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22672481/

10-14 16:28