【问题标题】:Viewcontroller not presented on first time run首次运行时未显示 Viewcontroller
【发布时间】:2013-10-17 20:09:46
【问题描述】:

我试图让视图控制器在第一次运行并且仅在第一次时呈现InitialViewController。然而,它与这条消息一起出现:

警告:尝试呈现不在窗口层次结构中的视图!

那我的代码有什么问题?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasRunBefore = [defaults boolForKey:@"FirstRun"];

if (!hasRunBefore) {
    [defaults setBool:YES forKey:@"FirstRun"];
    [defaults synchronize];
    UIViewController * InitialViewViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"view2"];
    [self presentViewController:InitialViewViewController animated:YES completion:NULL];
}

else
{
    NSLog (@"Not the first time this controller has been loaded");



}

【问题讨论】:

  • 您正在运行的这段代码在哪里?它应该在您应用的第一个呈现的 ViewController 中。

标签: ios objective-c uiviewcontroller


【解决方案1】:

您需要一个关键窗口。通常,您的代码中有[window makeKeyAndVisible];。这构成了窗口层次结构。

【讨论】:

  • 我应该把它放在哪里?
  • 通常在您的 AppDelegate 的 application:didFinishLaunchingWithOptions 中。见,例如here
  • K 看看那个。不管它是否有效,我都会回复你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 2011-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
相关资源
最近更新 更多