连续的UIAlertControllers - IOS - Swift

时间:2016-08-30 19:39:02

标签: ios swift uialertcontroller uialertaction

我想从Swift中的第一个警报控制器开始,提供一个连续的警报控制器

所以情景就是这样:

1)Alert_A提供2个选项:

  • a)现在Alert_B在选择此选项

  • 后也会解除Alert_A
  • b)现在Alert_C在选择此选项后也会解除Alert_A

2)Alert_B / Alert_C每个都有2个选项:

  • a)行动警报_B /行动警示_C

  • b)取消取消Alert_B / Alert_C

我在Apple文档中读到,不建议在警报中显示警报。

我还添加了一个指向警报层次结构的链接:

Alert Diagram

1 个答案:

答案 0 :(得分:1)

试试这个:

 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)

更好的方法:

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)