无法在UIAlertController中选择按钮顺序

问题描述 投票:9回答:2

我的印象是,如果正常动作是破坏性动作,而另一个是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,那么cancelActionleaveAction将是左边的按钮。此情况并非如此。我尝试以相反的顺序添加按钮,这也导致按钮以相同的顺序添加。

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

ios swift ios8 uialertcontroller
2个回答
8
投票

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


1
投票

从iOS9开始,UIAlertController上有一个preferredAction属性。它将行动放在右侧。来自docs:

指定首选操作时,警报控制器会突出显示该操作的文本以强调它。 (如果警报还包含取消按钮,则首选操作会收到突出显示而不是取消按钮。)

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

© www.soinside.com 2019 - 2024. All rights reserved.