Warning: attempt to present a UINavigationController to a ViewController whose view is not in the window hierarchy

I am working on an ios app and I am new to this. So, I get this error. What I'm trying to do is when the user first logs into the application, his username is stored locally on the phone using NSUserdefault.

Now, if they close the application and open it again, the application goes through the first view controller and in the viewdidload method checks if the USUserdefault is longer than zero, and if so, go to the main action by making a segue call, However, I get this error when I run it.

This is what I did on my storyboard. enter image description here

This is the code in the first controller

- (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]; } } 
+4
source share
1 answer

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]; } } 
+3
source

Source: https://habr.com/ru/post/1499174/


All Articles