0
votes

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
The error explains exactly what is going on. You are presenting a UIViewController onto the view hierarchy from a UIViewController that currently is not part of the view hierarchy. You haven't posted enough context for us to be able to help you debug the issue.Tim
I am launching the method that cannot be presented, from the ViewController.m file, which is presented when the user presses Play on the Start screen, I cannot see where-else to place it, if I try to do it when the user presses play, it still presents same problem?Hypergater

3 Answers

3
votes

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
votes

It's because the presenting view controller (i.e. self) is not added as child view controller. You can workaround that warning by presenting your vc on [UIApplication sharedApplication].keyWindow.rootViewController instead of self

0
votes

This is example of FormAlertView using Swift 2.3

  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)

}

You can easily to copy paste in your project viewController and run.