Attempt to present on ViewController whose view is not in the window hierarchy

时间:2015-06-30 13:25:23

标签: ios objective-c iphone

This is the code used on the main menu, when the user presses play, however when that screen is presented (ViewController.m) it shows the error

Warning: Attempt to present on whose view is not in the window hierarchy!

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

    }

}

In the AppDelegate.m file it shows this in the 'didFinishLaunchingWithOptions' section;

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

3 个答案:

答案 0 :(得分:3)

Try to write your code in view controllers viewDidAppear method because till that time your view is just created not added in view hierarchy.

答案 1 :(得分:1)

这是因为呈现视图控制器(即self)未添加为子视图控制器。 您可以通过在[UIApplication sharedApplication].keyWindow.rootViewController而不是self

上展示您的vc来解决该警告问题

答案 2 :(得分:0)

这是使用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中复制粘贴并运行。