xcode PushViewController 导致黑屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14929039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:47:38  来源:igfitidea点击:

PushViewController Leads To Black Screen

iphoneobjective-cxcode

提问by user2080841

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.

在 NavigationController 中使用此代码时,显示的场景只是一个黑屏。在故事板中,它显示内容,但不显示在手机上。

回答by shivam

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"

将设置视图控制器的标识符设置为“设置”

回答by Vishal

Use this:

用这个:

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

回答by Tristan Richard

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:

一个简单的方法是使用故事板实例化。您必须给您的视图控制器一个 Storyboard ID,然后使用以下代码:

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

When this is done simply push:

完成后,只需按下:

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

Hope this helps.

希望这可以帮助。

回答by Kumar C

Hi You've to set the top bar property (navigation bar) to inferredfor 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.

通常会发生这种情况,您从模态视图控制器执行 segue。

回答by JingJingTao

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

所以你可以使用每个人都建议的 xib 或故事板的名称进行初始化,这是有效的。

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

回答by Waseem Shah

try this

尝试这个

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

回答by svvoff

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