user4790024
user4790024

Reputation:

Present a ViewController although UIAlertController is in the view Hierarchy

I want to presentViewController when an alertController is presented.

I want such as if user presses the Ok button now i want to show the ViewController from AppDelegate on PushNotification.

I came to know on SO that UIAlertController is presented on different thread so want to present the ViewController after user presses Ok Button and moves on to main thread as below:

func presentRScreenFromRootViewController()
{

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let rViewController = storyboard.instantiateViewControllerWithIdentifier("rsh") as! R_SVC
    dispatch_async(dispatch_get_main_queue(),{

        self.window?.rootViewController?.presentViewController(rViewController, animated: true, completion: nil)


    });

}

But I get warning as:

 Attempt to present <BB.R_SVC: 0x15b983600>  on
 <SWRevealViewController: 0x15652df00> which is already presenting
 <UIAlertController: 0x15b4f2dd0>

This is because the viewcontroller is presenting alertcontroller.Other times its working well..My rootViewController changes and moves to next screen..But when there is UIAlertController i get warning as attempt to present viewcontroller which is not in the view hierarchy

What am i doing wrong?

Upvotes: 1

Views: 2393

Answers (1)

Chameleon
Chameleon

Reputation: 1608

when creating UIAlertController, place you VCpresenting code in the an UIAlertAction like so

let alert = UIAlertController(title: "your title", message "move to next VC?", preferredStyle: UIAlertController: UIAlertControllerStyle.Alert)
let action: UIAlertAction = UIAlertAction(title: "yes move to next VC", style: UIAlertActionStyle.Default) {action -> Void in 
    let rViewController = storyboard.instantiateViewControllerWithIdentifier("rsh") as! R_SVC
    self.presentViewController(rViewController, animated: true, completion: nil) })
alert.addAction(defaultAction)

self.presentViewController(alert, animated: true, completion: nil)

edit: Maybe try Dismiss all UIAlertControllers currently presented.

Upvotes: 1

Related Questions