为什么这个 UIAlertController 不显示?
Why doesn't this UIAlertController show?
在尝试实施 UI 警报时,我遇到了一些问题。我在 Xcode 8 beta 4 中使用 swift 3.0,我试图让一个按钮激活警报,一个按钮(取消)解除警报,另一个按钮(确定)执行一个操作作为 UI操作按钮会,但是我什至无法显示警报。
var warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .Alert)
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
warning.addAction(okAction) {
// this is where the actions to erase the content in the strings
}
warning.addAction(cancelAction)
self.presentViewController(warning, animated: true, completion: nil)
该代码与 Swift 不兼容 3. .Alert
现在是 .alert
。而presentViewController
方法则大不相同。
这应该有效。
let warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
//ok action should go here
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
warning.addAction(okAction)
warning.addAction(cancelAction)
present(warning, animated: true, completion: nil)
为什么在 addAction(okAction)
之后关闭而不是在创建警报时关闭?
希望对您有所帮助!
在尝试实施 UI 警报时,我遇到了一些问题。我在 Xcode 8 beta 4 中使用 swift 3.0,我试图让一个按钮激活警报,一个按钮(取消)解除警报,另一个按钮(确定)执行一个操作作为 UI操作按钮会,但是我什至无法显示警报。
var warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .Alert)
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
warning.addAction(okAction) {
// this is where the actions to erase the content in the strings
}
warning.addAction(cancelAction)
self.presentViewController(warning, animated: true, completion: nil)
该代码与 Swift 不兼容 3. .Alert
现在是 .alert
。而presentViewController
方法则大不相同。
这应该有效。
let warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
//ok action should go here
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
warning.addAction(okAction)
warning.addAction(cancelAction)
present(warning, animated: true, completion: nil)
为什么在 addAction(okAction)
之后关闭而不是在创建警报时关闭?
希望对您有所帮助!