Jump to content
3 posts in this topic

Recommended Posts

Okay so I'm trying to make a simple practice application in Xcode/interface builder 3. As part, I want the user to input a string into a text Field and then press a button which will trigger the code under the - (IBAction)checkPass section (codebox below). I want to use an if else statement to see if the entered string matches something, and if it does, display "correct." It's like a simple password. This is the .m file for my class that executes the action.

 

#import "Checker.h"

@implementation Checker
- (IBAction)checkPass:(id)sender {

 if ([textField stringValue] == @"fourthdimension") {

    [infoLabel setStringValue:@"Your password was correct!"];

 } else {

    [infoLabel setStringValue:@"Your password was incorrect!"];

 }

}

- (void)awakeFromNib {

 [infoLabel setStringValue:(NSString *)@"Enter password"];

}
@end

 

When I ran the application it gave me incorrect for everything I entered. After messing around with it, I came to the conclusion that it is the extra space in the text box that is causing the string not to equal "fourthdimension". Then, even more weird, I tested a simple code that says

 

[infoLabel setIntValue:[textField length];

 

And discovered apperently that everything I input has a length of 0...

 

Then I tried the whole same concept with integers.

 

- (IBAction)checkPass:(id)sender {

 int userInput = [textField intValue];

 if (userInput == 5) {

    [infoLabel setIntValue:1];

 } else {

    [infoLabel setIntValue:0];

 }

}

 

Same result. Returns 0 (synonomous with incorrect) for every integer I enter in the text field.

 

I hope it isn't too confusing, I'm really new with this, and haven't had alot of experience explaining things. I really hope someone can help.

Link to comment
https://www.insanelymac.com/forum/topic/142452-help-a-beginner-please/
Share on other sites

First of all, a NSString is an id, therefore you can't really do an == on it. A NSString can only == 0 or 1. 0 if it is not allocated (or equal to nil) and 1 if it is allocated (not nil).

 

Second, have you hooked up textField with interface builder?

 

Third: you need an extra bracket here: [infoLabel setIntValue:[textField length]];

×
×
  • Create New...