Can I include a NSUserDefault password test in AppDelegate to load a loginView?
- by Michael Robinson
I have a name and password in NSUserDefaults for login. I want to place a test in my AppDelegate.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 tabcontroller loading part:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
firstTab = [[FirstTab alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstTab];
[firstTab release];
secondTab = // EDITED FOR SPACE
thirdTab = // EDITED FOR SPACE
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavigationController, secondNavigationController, thirdNavigationController, nil];
[window addSubview:tabBarController.view];
[firstNavigationController release];
[secondNavigationController release];
[thirdNavigationController release];
[self logout];
[window makeKeyAndVisible];
Here is where the loginView.xib loads automatically:
- (void)logout {
loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[loginViewController release];
[tabBarController presentModalViewController:loginNavigationController animated:YES];
[loginNavigationController release];
}
I want to replace the above autoload with a test similar to below (that works) using IF-ELSE
- (void)logout {
if ([usernameLabel.text length] == 0 || [passwordLabel.text length] == 0)
{
loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[loginViewController release];
[tabBarController presentModalViewController:loginNavigationController animated:YES];
[loginNavigationController release];
}else
{
[window addSubview:tabBarController.view];}
Thanks in advance, I'm totally lost on this.