xcode 在 NSImageView 上设置图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6797770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:32:43  来源:igfitidea点击:

Setting an image on NSImageView

xcodecocoamacosnsimagensimageview

提问by Pedro Vieira

I'm having a problem with my program. Basically what i want is, i have 2 nssecuretextfield and i have a button. if both are equal, it shows one image on the nsimageview, if not it displays other image. This could be very easy, but i'm new to mac programming,

我的程序有问题。基本上我想要的是,我有 2 nssecuretextfield 和我有一个按钮。如果两者相等,则在 nsimageview 上显示一张图像,否则显示另一张图像。这可能很容易,但我是 mac 编程的新手,

the .h file:

.h 文件:

IBOutlet NSSecureTextField *textField;
IBOutlet NSSecureTextField *textField2;
IBOutlet NSImageView *imagem;
}

- (IBAction)Verificarpass:(id)sender; 

the .m file:

.m 文件:

- (IBAction)Verificarpass:(id)sender;
{
    NSString *senha1 = [textField stringValue];
    NSString *senha2 = [textField2 stringValue];
    NSImage *certo;
    NSImage *errado;
    certo = [NSImage imageNamed:@"Status_Accepted.png"];
    errado = [NSImage imageNamed:@"Error.png"];

    if (senha1 == senha2) {
    [imagem setImage:certo];
    }
    if (senha1 != senha2) {
        [imagem setImage:errado];
    }
}

can anyone help me please? i tried and it only displays 1 image, even if its right or wrong.

有人可以帮我吗?我试过了,它只显示 1 张图像,即使它是对的或错的。

回答by Yuji

You can't compare the contents of strings via ==or !=. That compares the pointer values (i.e. the address where the string object lives.)

您无法通过==或比较字符串的内容!=。比较指针值(即字符串对象所在的地址。)

Use

if ([senha1 isEqualToString:senha2]) {
    [imagem setImage:certo];
}else{
    [imagem setImage:errado];
}

instead.

反而。

Another unrelated advice: never start a method name with a capital letter. That's against Cocoa convention. Use verificarPassinstead.

另一个不相关的建议:永远不要以大写字母开头方法名称。这违反了 Cocoa 公约。使用verificarPass来代替。