Anton
Anton

Reputation: 3120

iOS Present Viewcontroller getting black screen

I am trying present ViewController I have created with StoryBoards:

 AuthViewController *authViewController = [[AuthViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:authViewController];
    UIViewController *vc  = [[[[UIApplication sharedApplication] windows] firstObject] rootViewController];
    [vc presentViewController:nav animated:YES completion:nil];

But getting it with black screen. What could be the issue?

Upvotes: 8

Views: 6279

Answers (4)

Manuel BM
Manuel BM

Reputation: 888

My problem was that the class, for example AuthViewController in the storyboard was a simple UIViewController, and in my swift class it was a UITabBarController. That's why it did not render anything.

Silly mistake, but I hope I can save some time for someone.

Upvotes: 0

Mahesh
Mahesh

Reputation: 204

You may refer to the snippet below for storyboard:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
AuthViewController *authViewController = [storyboard instantiateViewControllerWithIdentifier:@"**YOUR_STORYBOARD_ID**"];
[authViewController setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:authViewController animated:YES completion:nil];

Upvotes: 4

Karan Singla
Karan Singla

Reputation: 65

Is the ViewController for AuthViewController present in Storyboard? If yes, then you should pass it a Storyboard ID and should initiate your AuthViewController in this manner:

AuthViewController *authViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"_STORYBOARD_ID_"];

I hope this helps.

Upvotes: 2

Mehul Patel
Mehul Patel

Reputation: 23053

Alloc init AuthViewController does not mean that will create a view layout for controller.

Here view controller does not load its view that's why you are getting black screen. Use storyboard object and identifier of view controller to load its view.

Specify storyboard identifier in Storyboard of AuthViewController controller.

Add Storyboard ID and mark true for Use Storyboard ID option like in below image:

enter image description here

Now get AuthViewController controller object using below code:

AuthViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"AuthViewController"];

Upvotes: 13

Related Questions