1

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?

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39

1 Answers1

1

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.

Community
  • 1
  • 1
Chameleon
  • 1,608
  • 3
  • 21
  • 39
  • this issue arises all at runtime...for e.g if user gets alertviewcontroller without getting push notification ......and alertviewcontroller is still alive –  Oct 01 '15 at 06:40
  • did you try this method? it will close your UIAlertController then execute the code to present VC. – Chameleon Oct 01 '15 at 06:41
  • did you see my comment clearly ??? but dustin is there any way i can dismiss if there are alertcontroller currently in the view –  Oct 01 '15 at 06:42
  • no i am sorry but your comments aren't clear...dismiss? dismiss what? the AlertController? This method will present Alert, which currently has one button. when pressed...Alert will be dismissed and R_SVC will be presented. – Chameleon Oct 01 '15 at 06:44
  • no i want alertcontroller to dismiss if there are any presented –  Oct 01 '15 at 06:50