PushViewController Leads To Black Screen

2020-02-05 12:25发布

问题:

SettingsViewController *viewController = [[SettingsViewController alloc] init];
    [[self navigationController] pushViewController:viewController animated:YES];

When using this code in a NavigationController the scene shown is just a black screen. In storyboard it shows the content but it is not displayed on the phone.

回答1:

Try this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
SettingsViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"setting"];
[self.navigationController pushViewController:viewController animated:YES];

Set the settingviewcontroller's identifier to "setting"



回答2:

Use this:

SettingsViewController *viewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]]; 
[[self navigationController] pushViewController:viewController animated:YES];


回答3:

An easy way to do it is by instantiate with the storyboard. You must give your viewcontroller a Storyboard ID and then use the following code:

YourView *view = [self.storyboard instantiateViewControllerWithIdentifier:@"YourViewID"];

When this is done simply push:

[[self navigationController] pushViewController:view animated:YES];

Hope this helps.



回答4:

try this

  SettingsViewController *viewController = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]]; 
  [self.navigationController pushViewController:viewController animated:YES];


回答5:

I solved this problem by custom initialisation of destination view controller.

Your destination view controller:

-(id) initWithSender:(UINavigationController*)sender
{
        self=[sender.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCIdentifier"];
    if (self) {
        //custom initialisation
    }
    return self;
}

And using it:

DestinationVC *viewController = [[SettingsViewController alloc] initWithSender:self.navigationController];
[[self navigationController] pushViewController:viewController animated:YES];


回答6:

Hi You've to set the top bar property (navigation bar) to inferred for the view controller. In my case I previously set the top bar as "Translucent navigation bar"

Usually this happens you perform a segue from a modal view controller.



回答7:

so you could initialise using the name of the xib or storyboard as everyone is suggesting and that’s valid.

Although if you’re using storyboards and your initial viewcontroller is on the storyboard, they maybe rather do the following,

// Method in your initial viewcontroller 
- (void)youPushMethod { 

// Name your segue on your storyboard, e.g. SegueIndentifier 
[self performSegueWithIdentifier:@"SegueIndentifier" sender:self];
}

Then,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

     if ([segue.identifier isEqualToString:@“SegueIndentifier"]) {
     SettingsViewController *settingsViewController= segue.destinationViewController;
    }
}