【问题标题】:Attempt to present on ViewController whose view is not in the window hierarchy尝试在其视图不在窗口层次结构中的 ViewController 上呈现
【发布时间】:2015-09-17 07:45:34
【问题描述】:

这是在主菜单上使用的代码,当用户按下播放键时,当显示该屏幕时 (ViewController.m) 会显示错误

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

-(IBAction)play:(id)sender
{
    if(IS_IPAD)
    {
        ViewController *view_obj = [[ViewController alloc]initWithNibName:@"ViewController_iPad" bundle:[NSBundle mainBundle]];
        [self presentViewController:view_obj animated:YES completion:nil];

    }
    else if ([AISGlobal isIphone5thGeneration])
    {
        ViewController *view_obj = [[ViewController alloc]initWithNibName:@"ViewController_iPhone5" bundle:[NSBundle mainBundle]];
        [self presentViewController:view_obj animated:YES completion:nil];


    }
    else
    {
        ViewController *view_obj = [[ViewController alloc]initWithNibName:@"ViewController_iPhone" bundle:[NSBundle mainBundle]];
        [self presentViewController:view_obj animated:YES completion:nil];

    }

}

在 AppDelegate.m 文件中,它会在“didFinishLaunchingWithOptions”部分显示此内容;

if(IS_IPAD)
    {
         self.start_screen_obj = [[Start_screen alloc] initWithNibName:@"Start_screen" bundle:nil];
    }
    else if ([AISGlobal isIphone5thGeneration])
    {
        self.start_screen_obj = [[Start_screen alloc] initWithNibName:@"Start_screen_iphone5" bundle:nil];

    }
    else
    {
       self.start_screen_obj = [[Start_screen alloc] initWithNibName:@"Start_screen_iphone" bundle:nil];
    }

    self.start_screen_obj.view.multipleTouchEnabled = YES;

    self.window.rootViewController = self.start_screen_obj;

    [self.window makeKeyAndVisible];

【问题讨论】:

  • 错误解释了到底发生了什么。您正在从当前不属于视图层次结构的 UIViewController 将 UIViewController 呈现到视图层次结构上。您没有发布足够的上下文,我们无法帮助您调试问题。
  • 我正在从 ViewController.m 文件中启动无法呈现的方法,当用户在开始屏幕上按下 Play 时会呈现该文件,我看不到哪里可以放置它,如果我当用户按下播放时尝试这样做,它仍然会出现同样的问题?

标签: ios objective-c iphone


【解决方案1】:

尝试在视图控制器 viewDidAppear 方法中编写您的代码,因为在此之前您的视图刚刚创建并没有添加到视图层次结构中。

【讨论】:

    【解决方案2】:

    这是因为当前视图控制器(即self)没有添加为子视图控制器。 您可以通过在 [UIApplication sharedApplication].keyWindow.rootViewController 而不是 self 上显示您的 vc 来解决该警告

    【讨论】:

    • 嗨@arturdev 我把这个放在哪里?在 Appdelegate 中还是在按下播放后调用 View 时?
    • 打电话[self presentViewController:...时无处不在
    【解决方案3】:

    这是使用 Swift 2.3 的 FormAlertView 示例

      override func viewDidAppear(_ animated: Bool) {
        var loginTextField: UITextField?
        var passwordTextField: UITextField?
        let alertController = UIAlertController(title: "UIAlertController", message: "UIAlertController With TextField", preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
            print("Ok Button Pressed")
        })
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
            print("Cancel Button Pressed")
        }
        alertController.addAction(ok)
        alertController.addAction(cancel)
        alertController.addTextField { (textField) -> Void in
            // Enter the textfiled customization code here.
            loginTextField = textField
            loginTextField?.placeholder = "User ID"
        }
        alertController.addTextField { (textField) -> Void in
            // Enter the textfiled customization code here.
            passwordTextField = textField
            passwordTextField?.placeholder = "Password"
            //            passwordTextField?.secureTextEntry = true
        }
    
        //        present(alertController, animated: true, completion: nil)
    
    
    
        //          present(alertController, animated: true, completion: nil)
    
        self.present(alertController, animated: true, completion: nil)
    
    }
    

    您可以轻松地复制粘贴到您的项目 viewController 中并运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多