user2924482
user2924482

Reputation: 9120

iOS: presentViewController error Presenting view controllers on detached view controllers is discouraged

I'm trying to present a viewController programmatically:

- (void)viewDidLoad {
    [super viewDidLoad];
    NewViewController *vc = [[NewViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

But I'm getting this error:

Presenting view controllers on detached view controllers is discouraged 

and also I'm getting black screen on the iPhone.

This is how my storyboard and iPhone looks like:

enter image description here Any o you knows why I'm getting this error and also why I'm getting the black screen ?

I'll really appreciate your help.

Upvotes: 0

Views: 2377

Answers (1)

Nef10
Nef10

Reputation: 946

You are trying to present a view controller in viewDidLoad. This method is called on the time the view of the View Controller is being loaded from the Storyboard. At this time the View Controller is not yet displayed and therefore detached from the view of the app.

You might get the black screen because of this error.

To solve this problem to not call presentViewController in the viewDidLoad, call it later after the view is shown. For example you can call it in the viewDidAppear method of the view controller.

Upvotes: 2

Related Questions