views:

20

answers:

1

I have a name and password in NSUserDefaults for login. I have this in my 1stTab View.m class to test for presence and load a login/signup loginView.xib modally if there is no password or name stored in the app.

Here is the pulling of the defaults:

-(void)refreshFields {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    usernameLabel.text = [defaults objectForKey:kUsernameKey];
    passwordLabel.text = [defaults objectForKey:kPasswordKey];
{

Here is the Test:

- (void)viewDidAppear:(BOOL)animated {
        [self refreshFields];
        [super viewDidAppear:animated];

    if ([usernameLabel.text length] == 0 || [passwordLabel.text length] == 0)
{
    LoginViewController * vc = [[[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil] autorelease];
    [self presentModalViewController:vc animated: false];
}
else 
{
    [[self tableView ]reloadData];
}

}

Thanks in advance, I'm getting this error in the console:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key usernameLabel.'

A: 

The error you describe isn't related to NSUserDefaults; it is saying that your class (FirstTabViewController) isn't key value coding compliant for usernameLabel.

My guess is that you've defined an IBOutlet usernameLabel but not hooked it up in the xib or, for some other reason, usernameLabel is nil.

Olie
I put in a NSLog(@"userName Label: %@",usernameLabel.text); after the test, This returns as (null). This is empty, shouldn't the return be "nil" when first launching the app. I don't have a xib for this FirstTab, I took out the IBOutlet for the usernameLabel, I'm still getting the same error.
Michael Robinson
There wasn't an IBOutlet but I was reading elsewhere that old connections can cause this, I deleted an old label connection and now it will load. Thanks for pointing me in the right direction.
Michael Robinson