美文网首页
Swift Alert

Swift Alert

作者: tech_go | 来源:发表于2023-04-05 16:19 被阅读0次

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)

相关文章

网友评论

      本文标题:Swift Alert

      本文链接:https://www.haomeiwen.com/subject/ctzeddtx.html