UIAlertController - 动作顺序

时间:2017-08-18 06:34:08

标签: swift uialertcontroller

我想通过按下按钮显示警报窗口。通常的事情。但我很困惑,我试图显示按钮" Awesome"起初,但始终"取消"按钮保持第一。我该如何解决?

let alert = UIAlertController(title: "Hello world", message: "Testing", preferredStyle: .alert)
let action = UIAlertAction(title: "Awesome", style: .default){(_) in print("Awesome")}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

alert.addTextField(configurationHandler: nil)
alert.addAction(action)
alert.addAction(cancel)

alert.actions.forEach( { (action) in print( action.title! ) } )

present(alert, animated: true, completion: nil)

Irregular order of buttons

2 个答案:

答案 0 :(得分:2)

试试这样,它会对你有用。

let alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert)
let destructiveAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
    (result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "Awesome", style: UIAlertActionStyle.default) {
    (result : UIAlertAction) -> Void in

}
alertController.addAction(destructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

答案 1 :(得分:1)

更改订单将无效,因为取消按钮的默认位置仍然存在。

将取消按钮类型的操作样式更改为UIAlertActionStyleDefault而不是UIAlertActionStyleCancel。