junet
junet

Reputation: 53

Show two UIAlertController objects continuously using completion block

I'm trying to show two UIAlertController's instances continuously, which is like this code block below.

func showAlerts() {
    let alertA = UIAlertController(title: "Alert A", message: "This is alert a...", preferredStyle: .alert)
    let alertB = UIAlertController(title: "Alert B", message: "This is alert b...", preferredStyle: .alert)

    let alertButton1 = UIAlertAction(title: "OK", style: .default, handler: nil)
    let alertButton2 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alertA.addAction(alertButton1)
    alertA.addAction(alertButton2)
    alertB.addAction(alertButton1)
    alertB.addAction(alertButton2)

    self.present(alertA, animated: true) { 
        self.present(alertB, animated: true, completion: { 
            debugPrint("alerts are all shown")
        })
    }
}

I expect this code to show each alert continuously, which means alertB shows after alertA. But alertB doesn't appear as I expect, with warnings on console saying;

Warning: Attempt to present <UIAlertController: 0x7f7ffde0ace0>  on <ContinuousUIAlertController_Experiment.ViewController: 0x7f7ffdd092d0> which is already presenting <UIAlertController: 0x7f7ffde09f90>

If I remember correctly, multiple UIAlertController objects cannot be existed at the same time. So I somehow understand what the warning above tells.

So, then, how can I implement continuous alert showing using completion of UIViewController::present(_:animated:completion:) or with nearly the same logic? (I prefer not to use UIAlertAction's handler)

If there is a solution, please let me know.
I'm struggling with this problem for a few days and I've not addressed yet.

Upvotes: 0

Views: 1174

Answers (2)

Lakshmi C
Lakshmi C

Reputation: 29

You can do this by using topController. Just call topController.present instead of self.present. By doing this way, you can present an alert over an alertController.

Get the top controller as such:

func getTopController() -> UIViewController? {
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }
    return nil
}

And use it as,

getTopController().present(alertA, animated: true) { 
    getTopController().present(alertB, animated: true, completion: { 
        print("alerts are all shown")
    })
}

Upvotes: -1

Hasya
Hasya

Reputation: 9898

You can't show 2 uialettcontrol at 1time.

But you can show 2nd uialettcontrol on 1st one's uialeraction.

For example

let alertController = UIAlertController(title: "iOScreator", message:
            "Hello, world!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Destructive,handler: { action in
            self.pressed()
        }))

func pressed()
{
    print("you pressed")
}

On pressed event you can write code for 2nd uialettcontrol.

Upvotes: 0

Related Questions