Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform Segue on ViewDidLoad

In iOS 5 I have a Storyboard with a modal view controller, that I would like to display if its the user's first time in the app, after that I would like to skip this view controller.

I set an NSDefault key to handle this but when I check to see if this is set and then use performSegueWithIdentifier to initiate the segue, nothing happens. If i put this segue behind a button it works fine...

like image 754
adam0101 Avatar asked Nov 22 '11 03:11

adam0101


People also ask

Can viewDidLoad be called multiple times?

-viewDidLoad will be called once whenever the view controller needs to load its view hierarchy. Obviously, that'll happen the first time that the controller accesses its view. If the view controller later unloads its view, then -viewDidLoad will be called again the next time the view is loaded.

Does viewDidLoad get called before viewWillAppear?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

How many times does viewDidLoad get called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

What is the difference between viewDidLoad and viewDidAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.


1 Answers

I answered a similar question where the developer wanted to show a login screen at the start. I put together some sample code for him that can be downloaded here. The key to solving this problem is calling things at the right time if you want to display this new view controller, you will see in the example you have to use something like this

- (void)viewDidAppear:(BOOL)animated {     [super viewDidAppear:animated];      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];     UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];     [vc setModalPresentationStyle:UIModalPresentationFullScreen];      [self presentModalViewController:vc animated:YES]; } 

I also have an explanation of how segues and storyboards work that you can see here

like image 120
Scott Sherwood Avatar answered Sep 18 '22 12:09

Scott Sherwood