ios - 为什么我的警报无法执行阻止且无法解除?
问题描述
我使用UIAlertController创建了一个警报并在其中添加了一些操作。但是当我触摸它的按钮时,什么都没有改变。(包括取消按钮)所以我只能重新启动这个应用程序才能解决这个问题。
let alert = UIAlertController(title: “my alert”, message: “some message...”, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: “button1”, style: .default, handler: { (_) in
print(“button 1 up inside”)
}))
alert.addAction(UIAlertAction(title: “button2”, style: .default, handler: { (_) in
print(“button 2 up inside”)
}))
alert.addAction(UIAlertAction(title: “cancel”, style: .cancel, handler: { (_) in
}))
self.present(alert, animated: true, completion: nil)
Xcode 10 内置。
解决方案
我在使用 Sprite Kit 的应用程序中遇到了类似的问题。这是解决方案:
let connectActionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
connectActionSheet.addAction(UIAlertAction(title: "Button1", style: .default, handler: { (action:UIAlertAction) in
//Code if this button is pressed
}))
connectActionSheet.addAction(UIAlertAction(title: "Button2", style: .default, handler: { (action:UIAlertAction) in
//Code if button2 is pressed
}))
connectActionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
let vc = self.view?.window?.rootViewController
if vc?.presentedViewController == nil {
vc?.present(connectActionSheet, animated: true, completion: nil)
}
最后 4 行是使它起作用的原因。我希望这可以帮助你。
推荐阅读
- node.js - 我的 node.js 服务器的域名是什么?
- encryption - 软件中的加密字符串
- sql-server - 我可以在 SQL 中将变量设置为数据类型长度吗?
- mule - 比较 mule 4 中的两个值
- python - 由于校验和,PIP 无法下载任何包
- indexing - Apache Solr - 需要两次索引相同的文档才能使纯文本查询正常工作
- html - 更正 R 中 HTML 表格中错放的单元格
- c# - C# WPF 将动画 gif 设置为背景
- javascript - 在鼠标悬停时加载javascript链接?
- android - 黑色(夜间模式)瓷砖出现在自定义 rastersource react-native-mapbox-gl 的高缩放级别