【问题标题】:Unable to choose order of buttons in UIAlertController无法在 UIAlertController 中选择按钮的顺序
【发布时间】:2015-08-27 22:03:27
【问题描述】:

我的印象是,如果正常操作是破坏性操作,而另一个是其 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


    【解决方案1】:

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

    【讨论】:

    • 这是正确的。显然,iOS 喜欢把取消语义按钮放在首位。
    【解决方案2】:

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

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

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

    【讨论】:

    • 这就是我需要的:右侧加粗字体的正面按钮,左侧为系统样式的取消按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2018-07-08
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多