Mike Me
Mike Me

Reputation: 2392

Consecutive UIAlertControllers - IOS - Swift

I would like to present a consecutive Alert Controllers starting with an action from the first Alert Controller in Swift.

So the Scenario is this:

1) Alert_A is presented with 2 options:

2) Alert_B/Alert_C will have 2 options each:

I've read in the Apple doc that it is not recommended to present an alert within an alert.

I also added a link to the hierarchy of Alerts:

Alert Diagram

Upvotes: 0

Views: 566

Answers (1)

Carlo
Carlo

Reputation: 835

Try with this:

 let alertController = UIAlertController(title: "Choose", message: "Choose one of two alert options", preferredStyle: UIAlertControllerStyle.Alert)
        let Alert1 = UIAlertAction(title: "Alert1", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in

let alertController = UIAlertController(title: "Alert1", message: "You chose Alert1", preferredStyle: UIAlertControllerStyle.Alert)
        let Action1 = UIAlertAction(title: "Action1", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
            /////////YOUR Action1////////
        }
        let CancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
        }

        alertController.addAction(Action1)
        alertController.addAction(CancelAction)


    self.presentViewController(alertController, animated: true, completion: nil)
        }
        let Alert2 = UIAlertAction(title: "Alert2", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in

let alertController = UIAlertController(title: "Alert2", message: "You chose Alert2", preferredStyle: UIAlertControllerStyle.Alert)
        let Action2 = UIAlertAction(title: "Action2", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
            /////////YOUR Action2////////
        }
        let CancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
        }

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


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

Better Method:

let alertController = UIAlertController(title: "Choose", message: "Action1 or Action2?", preferredStyle: UIAlertControllerStyle.Alert)
    let Action1 = UIAlertAction(title: "Action1", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
        ///////Action1///////
    }
    let Action2 = UIAlertAction(title: "Action2", style: UIAlertActionStyle.Default) { (result : UIAlertAction) -> Void in
        //////Action2///////
    }
    let CancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (result : UIAlertAction) -> Void in
    }

    alertController.addAction(Action1)
    alertController.addAction(Action2)


      alertController.addAction(CancelAction)

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

Upvotes: 1

Related Questions