0

I'm really new to Mac application building and I'm trying out some different techniques. I'm trying to figure out how to put in a basic login (username and password) screen into my app. In my method I currently have this, attempting to use the if else statements (the set background colour is just a temp replacement for opening the next window).

- (IBAction)loginb:(id)sender {
if (m_username == [@"haseo98" NSString]){
    [_window setBackgroundColor:[NSColor redColor]];
}
 else [_window setBackgroundColor:[NSColor blackColor]];
}

The main problem in my opinion is with this line:

 if (m_username == [@"haseo98" NSString]){

How do I get XCode to read what's in the NSTextfield and compare it to "haseo98"?

dda
  • 6,030
  • 2
  • 25
  • 34
haseo98
  • 837
  • 2
  • 10
  • 13

1 Answers1

1

NSTextField is a NSControl, so you can use -stringValue to get the text content:

NSString *name = [usernameTextfield stringValue];

To compare the strings, use -isEqualToString::

if ([name isEqualToString:@"haseo98"]){

Note that comparing strings using == doesn't usually do what you want and that string literals (@"text") are already NSString instances.

Community
  • 1
  • 1
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236