Setting up an image on NSImageView

I have a problem with my program. Basically what I want, I have 2 nssecuretextfield and I have a button. if both are equal, it shows one image in the nsimageview view, if it does not display another image. It can be very easy, but I'm new to programming on Mac,

.h file:

IBOutlet NSSecureTextField *textField; IBOutlet NSSecureTextField *textField2; IBOutlet NSImageView *imagem; } - (IBAction)Verificarpass:(id)sender; 

.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? I tried and it only displays 1 image, even if it is right or wrong.

+4
source share
1 answer

You cannot compare the contents of strings with == or != . This compares the values โ€‹โ€‹of the pointer (i.e., the address where the string object lives.)

Use

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

instead of this.

Another unrelated tip: never capitalize a method name. This is against the Cocoa agreement. Use verificarPass instead.

+4
source

All Articles