首页 > 解决方案 > 为什么我的警报无法执行阻止且无法解除?

问题描述

我使用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 内置。

标签: iosswiftuialertcontroller

解决方案


我在使用 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 行是使它起作用的原因。我希望这可以帮助你。


推荐阅读