Swift中可以通过UIAlertController和UIAlertAction来创建UIAlertview,示例代码如下:
// 创建UIAlertController
let alertController = UIAlertController(title: "提示", message: "确定要删除吗?", preferredStyle: .alert)
// 创建UIAlertAction
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: .destructive) { (action) in
// 在这里添加删除操作
}
// 将UIAlertAction添加到UIAlertController中
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
// 显示UIAlertController
self.present(alertController, animated: true, completion: nil)
在Swift中,可以使用UIAlertController来创建Alert和Action Sheet。Alert是一种弹出窗口,用于向用户显示一条消息、询问问题或提供选项。Action Sheet是一种在屏幕底部弹出的卡片,用于提供一组选项。
以下是创建Action Sheet的示例代码:
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
// Action 1 code
}
let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
// Action 2 code
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// Cancel code
}
alertController.addAction(action1)
alertController.addAction(action2)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
网友评论