2

Is it possible to add UIAlert view inside UIAlertAction ?

Because when I tried to add UIAlert view inside UIAlertAction, it says

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

Here's my code.

let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:
    {
        action in
        let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let navigationController = UINavigationController.init(rootViewController: myViewController)
        appDelegate.window?.rootViewController = navigationController
        appDelegate.window?.makeKeyAndVisible()

        if (statement here == 1) {
            let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
            myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
            self.present(myAlert, animated: true, completion: nil)
            return
        }
    }
)
myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)
AlotJai
  • 147
  • 4
  • 18

2 Answers2

2

Try to present AlertController on navigationController.

Changed line

self.present(myAlert, animated: true, completion: nil)

With

navigationController.present(myAlert, animated: true, completion: nil)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Let say my code above is inside SecondViewController and if (statement == 1){} is using your code above to make it work. But what if I have another else "if (statement == 2) {}" below it and I want to display alert without go to rootViewController. What I mean is it stay in SecondViewController Is it possible ? – AlotJai Apr 05 '17 at 11:41
  • @AlotJai Then you need to write these two line `appDelegate.window?.rootViewController = navigationController appDelegate.window?.makeKeyAndVisible()` inside `if (statement == 1){}` – Nirav D Apr 05 '17 at 12:06
0

Try wrapping your call to the second alert in a function then call that function.

Like this.

let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: { action in

    let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let navigationController = UINavigationController.init(rootViewController: myViewController)
    appDelegate.window?.rootViewController = navigationController
    appDelegate.window?.makeKeyAndVisible()

    if (statement here == 1) {
        self.callAlert()
    }
})

myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)

func callAlert() {
    let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
    myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
    self.present(myAlert, animated: true, completion: nil)
}
Luke Chase
  • 322
  • 1
  • 18