Multiple UIAlertControllers to show one after the other in Swift

3.8k views Asked by At

I have set up an alertcontroller for my app, the way it works is if section score is higher than 10, you get a ui alert.

Now My problem is that if i have 2 or 3 sections over 10, I only get the first UIalert to show, id like to see all of them one after the other (if this sutuation happens

here is my code :

func SectionAlert () {

    var message1 = NSLocalizedString("Section 1 score is now ", comment: "");
    message1 += "\(section1score)";
    message1 += NSLocalizedString(" please review before continuing", comment: "1");

    var message2 = NSLocalizedString("Section 2 score is now ", comment: "");
    message2 += "\(section2score)";
    message2 += NSLocalizedString(" please review before continuing", comment: "2");

    var message3 = NSLocalizedString("Section 3 score is now ", comment: "");
    message3 += "\(section3score)";
    message3 += NSLocalizedString(" please review before continuing", comment: "3");

    if (section1score >= 10){
        let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 1 score is over 10", comment: ""),
            message: " \(message1)",
            preferredStyle: .Alert)

        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            action -> Void in }

        alertController.addAction(OKAction)
        self.presentViewController(alertController, animated: true, completion: nil)

    } else if (section2score >= 10){
        let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 2 Score is over 10", comment: ""),
            message: "\(message2)",
            preferredStyle: .Alert)

        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            action -> Void in }

        alertController.addAction(OKAction)
        self.presentViewController(alertController, animated: true, completion: nil)

    } else if (section3score >= 10){
        let alertController: UIAlertController = UIAlertController(title: NSLocalizedString("Section 3 Score is over 10", comment: ""),
            message: "\(message3)",
            preferredStyle: .Alert)

        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            action -> Void in }

        alertController.addAction(OKAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

any ideas ??

thanks !

2

There are 2 answers

0
Jp4Real On BEST ANSWER

OK, I've figured it out, what I've done is get the code to be ran when I press OK on the view , so that it checks the other sections then pops another one if need be.

I've putted it in after

action -> Void in

thanks a lot

6
Aaron Brager On

The primary problem is that you're using else if. The section two and three conditionals won't be tested unless the prior ones evaluate to false.

So you want to change this:

if (section1score >= 10){
    // …
} else if (section2score >= 10){
    // …
} else if (section3score >= 10){
    // …
}

To look more like this:

if (section1score >= 10){
    // …
}

if (section2score >= 10){
    // …
}

if (section3score >= 10){
    // …
}

That said, you won't be able to present three view controllers at the same time. You may want to update your code to combine the messages into one alert. (This would be a better user experience than seeing three modal alerts appear at the same time.)