无法在UIAlertController中选择按钮顺序

时间:2015-08-27 22:03:28

标签: ios swift ios8 uialertcontroller

我的印象是,如果正常操作是破坏性操作,而另一个是UIAlertController中的取消操作,则破坏性操作应位于左侧,取消应位于右侧。

如果正常动作不是破坏性的,则正常动作应在右侧,取消应在左侧。

那说,我有以下几点:

var confirmLeaveAlert = UIAlertController(title: "Leave", message: "Are you sure you want to leave?", preferredStyle: .Alert)

let leaveAction = UIAlertAction(title: "Leave", style: .Destructive, handler: {
        (alert: UIAlertAction!) in

        //Handle leave

    }
)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

confirmLeaveAlert.addAction(leaveAction)
confirmLeaveAlert.addAction(cancelAction)

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

我的印象是,如果我先添加leaveAction,那么cancelAction leaveAction将是左侧的按钮。此情况并非如此。我尝试以相反的顺序添加按钮,这也导致按钮按相同的顺序添加。

我错了吗?有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:8)

我的解决方案是对.Default使用.Cancel样式而不是cancelAction

答案 1 :(得分:0)

自iOS9起,UIAlertController上有一个preferredAction属性。它将动作放在右侧。来自文档:

  

当您指定首选操作时,警报控制器会突出显示该操作的文本以使其重点突出。 (如果警报中还包含取消按钮,则首选操作会收到突出显示的信息,而不是取消按钮。)

     

您分配给此属性的操作对象必须已经添加到警报控制器的操作列表中。在使用addAction:方法添加对象之前,将对象分配给该属性是程序员错误。

相关问题