Setting an image on NSImageView

Pedro Vieira picture Pedro Vieira · Jul 23, 2011 · Viewed 12.5k times · Source

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,

the .h file:

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

- (IBAction)Verificarpass:(id)sender; 

the .m file:

- (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.

Answer

Yuji picture Yuji · Jul 23, 2011

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 verificarPass instead.