// 方法1
let alertSheet = UIActionSheet(title: alertTitle, delegate: nil, cancelButtonTitle: alertOK, destructiveButtonTitle: alertCancel)
alertSheet.showInView(self.view)
// 方法2
// 实例化时添加代理对象,同时注意添加协议
let alertSheet = UIActionSheet(title: alertTitle, delegate: self, cancelButtonTitle: alertOK, destructiveButtonTitle: alertCancel, otherButtonTitles: "警告", "提示", "通告")
alertSheet.showInView(self.view)
// 添加协议
class ViewController: UIViewController, UIActionSheetDelegate {
override func viewDidLoad() {
...
}
...
}
// 代理方法
// MARK: UIActionSheetDelegate
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
let buttonTitle = actionSheet.buttonTitleAtIndex(buttonIndex)
if buttonTitle == alertCancel
{
print("你点击了退出")
}
else if buttonTitle == alertOK
{
print("你点击了确定")
}
else
{
print("你点击了其他")
}
}
// 方法3
// 1 实例化
let alertSheet = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.ActionSheet)
// 2 命令(样式:退出Cancel,警告Destructive-按钮标题为红色,默认Default)
let cancelAction = UIAlertAction(title: alertCancel, style: UIAlertActionStyle.Cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)
let archiveAction = UIAlertAction(title: alertOK, style: UIAlertActionStyle.Default, handler: {
action in
print("OK")
})
alertSheet.addAction(cancelAction)
alertSheet.addAction(deleteAction)
alertSheet.addAction(archiveAction)
// 3 跳转
self.presentViewController(alertSheet, animated: true, completion: nil)
Swift - 操作表(UIActionSheet)的用法,也叫底部警告框:
1,下面创建一个操作表(或叫底部警告框)并弹出显示
import UIKit
class ViewController: UIViewController,UIActionSheetDelegate
{
override func viewDidLoad() {
super.viewDidLoad()
let actionSheet=UIActionSheet()
//actionSheet.title = "请选择操作"
actionSheet.addButtonWithTitle("取消")
actionSheet.addButtonWithTitle("动作1")
actionSheet.addButtonWithTitle("动作2")
actionSheet.cancelButtonIndex=0
actionSheet.delegate=self
actionSheet.showInView(self.view);
}
func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
print("点击了:"+actionSheet.buttonTitleAtIndex(buttonIndex)!)
}
}