presentViewController shows black page

2020-02-09 07:24发布

问题:

- (IBAction)submitButtonClicked:(id)sender{
    NSLog(@"here");
    SuccessOrFailViewController *sfvc = [[SuccessOrFailViewController alloc] init];
    [self presentViewController:sfvc animated:NO completion:NULL];
}

I am trying to open a new page when a user clicks the submit button in the app. However, this opens completely black screen with nothing on it. How do I make it open the viewController instead ?

回答1:

You are not entering the nib name for the user Interface:

SuccessOrFailViewController *sfvc = [[SuccessOrFailViewController alloc] initWithNibName:@"SuccessOrFailViewController" bundle:nil];
[self presentViewController:sfvc animated:NO completion:NULL];

For storyboard this is the way to go:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
SuccessOrFailViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:@"SuccessOrFailViewController"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:sfvc animated:YES];


回答2:

Swift

var next = self.storyboard?.instantiateViewControllerWithIdentifier("DashboardController") as! DashboardController
self.presentViewController(next, animated: true, completion: nil)

don't forget to set ViewController StoryBoard Id in StoryBoard -> identity inspector



回答3:

Incase anyone else finds this, make sure you haven't set self.view.backgroundColor = UIColor.clearColor() in the view to be shown... This solved it for me.



回答4:

For Storyboard iOS7

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


回答5:

I've had this happen after adding and deleting View Controllers in the Storyboard and forgetting to update the View Controller with the latest Storyboard identifier.

For example, if you are programmatically moving to a new View Controller, like this (note Identifier in the code is "FirstUserVC"):

let firstLaunch = (storyboard?.instantiateViewControllerWithIdentifier("FirstUserVC"))! as UIViewController
self.navigationController?.pushViewController(firstLaunch, animated: true)

The make sure in Interface Builder you have the Storyboard ID set like this with FirstUserVC listed in the Identity for Storyboard ID: