0

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.

user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

2

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.

Nef10
  • 926
  • 2
  • 16
  • 26
  • I change it to viewDidApper and I have no errors but still have the black screen. Any ideas of why? – user2924482 May 10 '17 at 23:22
  • Have you checked what is shown in the view debugger? Have you added breakpoints to the relevant methods of `NewViewController` to see if it was shown correctly? – Nef10 May 10 '17 at 23:24
  • Yes and all methods are been call. viewWillAppear and viewDidLoad and viewDidAppear. But the screen is black – user2924482 May 10 '17 at 23:29
  • you typically don't create a view controller with [[NewViewController alloc] init]; how is the view getting created? – combinatorial May 10 '17 at 23:33
  • Yeah, this is the problem. Because you use Storyboards, why don't you use segues? If you don't want to tou can have a look at [this SO answer](http://stackoverflow.com/a/15214504/3386893) to see how to correctly create a View Controller from the Storyboard. – Nef10 May 10 '17 at 23:36