开发者

IF statment not checking condition on first run

I'm new to programming and I have an app that has a login view on start up and request the user to enter their name which is used throughout out the program. Once they enter their name and log in they are presented with the main menu view. Their name is saved using NSUserdefaults.

The idea is that they will only have to login once (or again if they logout) so they should only see the login view the first time they run the app however once the app is started again it still shows the login screen and also you have to press the login button twice before you are taken to the main menu.

I know that the app is storing the details because it is used thought the app but I cant work out why. Here is my code. If someone could help it would be greatly appreciated.

-(IBAction)LogInButton:(id)sender
{
     NSString *tempStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];

     if(tempStr.length==0) 
     {
          NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
          [pref开发者_StackOverflow社区s setObject:Name.text forKey:@"UserName"];
          [prefs synchronize];

          LogInView *Logview = [[LogInView alloc] initWithNibName:@"LogInView" bundle:nil];
          [self presentModalViewController:Logview animated:YES];
     }
     else 
     {
          MainMenuView *mainview = [[MainMenuView alloc] initWithNibName:@"MainMenuView"      bundle:nil];
          [self presentModalViewController:mainview animated:YES];
     }
}


Judging by your description what you want is

  • On viewDidLoad check to see if the user is logged in
    • If YES show the MainMenu
    • If NO show the LogInView

The code may look like this

- (void)viewDidLoad
{
  [super viewDidLoad];

  [self showCorrectController];
}

The show correct controller method could look like this

- (void)showCorrectController
{
  UIViewController *viewController = nil;

  if ([self isLoggedIn]) {
    viewController = [[MainMenuView alloc] init];
  } else {
    viewController = [[LogInView alloc] init];
  }

  [self presentModalViewController:viewController animated:YES];
  [viewController release]; viewController = nil;
}

A convenience method is called isLoggedIn which looks like this

- (BOOL)isLoggedIn
{
  // The double negation just means we get a boolean response
  return !![[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
}

Now edit your original method to something like this

-(IBAction)LogInButton:(id)sender
{
  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  [prefs setObject:Name.text forKey:@"UserName"];
  [prefs synchronize];
  [self showCorrectController];
}

There are quite a few things that could be done to tidy this up a lot but this should be a start to get you going.

A word of caution on your naming of things. The convention is to start method and variable names with lowercased letters. Classes and constants start with uppercase letters.


It looks like the first time in:

  • The login screen shows up
  • The user presses login (and this method you're showing gets called)
  • The saved value isn't initially set, so this evaluates to true: if(tempStr.length==0)
  • You save the new value
  • You display another login screen

But I don't think you're showing all the code. What runs when the app launches?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜