Reputation: 1987
I'm working on an ios app and I'm new to it. So, I'm getting this error. What I'm trying to do is when a user first registers on the app, their username is stored locally on the phone using NSUserdefault.
Now if they close the app and reopen it the app goes through the first view controller and in the viewdidload method it checks if the length of the USUserdefault is greater than zero and if yes go to the main activity by perform a segue call. However I get that error when I run it.
This is what I did on my storyboard.
This is the code in the firstviewcontroller
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];
if(user.length>0){
NSLog(@"Going straight to main %s", "Yes");
[self performSegueWithIdentifier:@"startMain" sender:self];
}
}
Upvotes: 4
Views: 3516
Reputation: 6918
All you have to do is move the method to viewDidAppear:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];
if(user.length>0){
NSLog(@"Going straight to main %s", "Yes");
[self performSegueWithIdentifier:@"startMain" sender:self];
}
}
Upvotes: 5