开发者

If statement not checking condition on first run

just a little problem. My app stores some login info using NSUserDefaluts and users an IF statement to check if the file exist, if so it load second view, if not go to login screen.

Once the user enters Name, email etc they press go button which saves the details and takes to second view however the user has to press the go button twice before the app takes them to the second screen. I think the IF statement is not finding the saved details on the first run and therefore the button needs to be pressed a second time.

How can I over come this as it will be very frustrating for the user. I have included my code below. An explanation with code would be great appreciated.

-(IBAction)LogInButton:(id)sender {

NSString *tempStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];

  if (tempStr == nil || [tempStr isEqualToString:@""]) {

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:Name.text forKey:@"UserName"];
    [prefs synchronize];


    ClubSearchViewController *CSearch = [[ClubSearchViewControll开发者_如何学编程er alloc] initWithNibName:@"ClubSearchViewController" bundle:nil];
    [self presentModalViewController:CSearch animated:YES];


  }
  else {
     SearchMenu *SMenu = [[SearchMenu alloc]initWithNibName:@"SearchMenu" bundle:nil];
     [self presentModalViewController:SMenu animated:YES];
  }
}

Thanks in advance.


The code has to go in one of the two paths and present something, if it's not coming up on the first press that means it's not hitting that code. Set a breakpoint to make sure it is.

The if statement should really be:

if ( tempStr.length == 0 )

Since you can pass messages to nil you get zero back if the string is empty or nil. It's a bit simpler and takes less work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜