iOS Present Viewcontroller getting black screen

2019-04-28 06:21发布

问题:

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?

回答1:

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:

Now get AuthViewController controller object using below code:

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


回答2:

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];


回答3:

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.



回答4:

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.