是否可以使用处理程序触发其他警报?
func jokeFinal() {
let alert = UIAlertController(title: "Never Mind", message: "It's Pointless", preferredStyle: .alert)
let action = UIAlertAction(title: "Hahahahahaha", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
func joke() {
let alert = UIAlertController(title: "A broken pencil", message: "...", preferredStyle: .alert)
let action = UIAlertAction(title: "A broken pencil who?", style: .default, handler: jokeFinal())
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
@IBAction func nock() {
let alert = UIAlertController(title: "Knock,Knock", message: "..", preferredStyle: .alert)
let action = UIAlertAction(title: "Who's there??", style: .default, handler: joke())
alert.addAction(action)
present(alert, animated: true, completion: nil)
}我试图使用UIAlertAction的处理程序来调用另一个UIAlert。有可能吗?
我收到以下错误:
不能将类型“()”的值转换为预期的参数类型(UIAlertAction) -> Void)?)
发布于 2017-11-18 02:51:50
当然可以!这是可能的。试着做这样的事:
let alertController = UIAlertController.init(title: "Title", message: "Message", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Title", style: .default, handler: { (action) in
self.someFunction()
}))
self.present(alertController, animated: true, completion: nil)以下是您的功能:
func someFunction() {
let alertController = UIAlertController.init(title: "Some Title", message: "Some Message", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Title For Button", style: .default, handler: { (action) in
// Completion block
}))
self.present(alertController, animated: true, completion: nil)
}这是你的问题线:
let action = UIAlertAction(title: "Who's there??", style: .default, handler: joke())您可以轻松地将其更改为:
let action = UIAlertAction(title: "Who's there??", style: .default, handler: { (action) in
// Completion block
})希望能帮上忙!
发布于 2017-11-18 02:30:40
处理程序不调用函数。这是一种功能。
所以,例如,你可以这样做。更改…的声明
func jokeFinal() {至
func jokeFinal(_ action: UIAlertAction) {然后改变
handler: jokeFinal()至
handler: jokeFinal诸若此类。
https://stackoverflow.com/questions/47361837
复制相似问题