首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用UIAlertAction的“处理程序”来调用另一个UIAlertAction?

如何使用UIAlertAction的“处理程序”来调用另一个UIAlertAction?
EN

Stack Overflow用户
提问于 2017-11-18 02:21:59
回答 2查看 4.9K关注 0票数 0

是否可以使用处理程序触发其他警报?

代码语言:javascript
复制
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)?)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-18 02:51:50

当然可以!这是可能的。试着做这样的事:

代码语言:javascript
复制
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)

以下是您的功能:

代码语言:javascript
复制
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)
}

这是你的问题线:

代码语言:javascript
复制
let action = UIAlertAction(title: "Who's there??", style: .default, handler: joke())

您可以轻松地将其更改为:

代码语言:javascript
复制
let action = UIAlertAction(title: "Who's there??", style: .default, handler: { (action) in
        // Completion block
})

希望能帮上忙!

票数 3
EN

Stack Overflow用户

发布于 2017-11-18 02:30:40

处理程序不调用函数。这是一种功能。

所以,例如,你可以这样做。更改…的声明

代码语言:javascript
复制
 func jokeFinal() {

代码语言:javascript
复制
 func jokeFinal(_ action: UIAlertAction) {

然后改变

代码语言:javascript
复制
 handler: jokeFinal()

代码语言:javascript
复制
 handler: jokeFinal

诸若此类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47361837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档