Dav*_*vid 10 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)
Run Code Online (Sandbox Code Playgroud)
我的印象是,如果我添加第leaveAction一个,那么它cancelAction就是leaveAction左边的按钮.此情况并非如此.我尝试以相反的顺序添加按钮,这也导致按钮以相同的顺序添加.
我错了吗?有没有办法实现这个目标?
从 iOS9 开始,UIAlertController 上有一个preferredAction属性。它将动作放在右侧。从文档:
当您指定首选操作时,警报控制器会突出显示该操作的文本以强调它。(如果警报还包含取消按钮,则首选操作会收到突出显示而不是取消按钮。)
您分配给此属性的操作对象必须已添加到警报控制器的操作列表中。在使用 addAction: 方法添加对象之前将对象分配给该属性是程序员错误。