Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么UIAlertAction';需要一个引用UIAlertAction的参数吗?_Swift_Syntax_Uibutton_Handler_Uialertaction - Fatal编程技术网

Swift 为什么UIAlertAction';需要一个引用UIAlertAction的参数吗?

Swift 为什么UIAlertAction';需要一个引用UIAlertAction的参数吗?,swift,syntax,uibutton,handler,uialertaction,Swift,Syntax,Uibutton,Handler,Uialertaction,我正在使用一个简单的按钮,当按下时,它会运行我创建的一个函数 最初,我是这样编写代码的: func askQuestion() { // runs code to ask a question } @IBAction func buttonTapped(_ sender: UIButton) { let ac = UIAlertController(title: title, message: "You tapped the button.", preferredStyle

我正在使用一个简单的按钮,当按下时,它会运行我创建的一个函数

最初,我是这样编写代码的:

func askQuestion() {
     // runs code to ask a question
}

@IBAction func buttonTapped(_ sender: UIButton) {
     let ac = UIAlertController(title: title, message: "You tapped the button.", preferredStyle: .alert)
     ac.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion))
    present(ac, animated: true)
但这会返回一个错误:

Cannot convert value of type '() -> ()' to expected argument type '((UIAlertAction) -> Void)?'
将以下参数添加到askQuestion()时,该问题已修复:

为什么传入UIAlertAction的处理程序方法要求它接受UIAlertAction参数?这似乎是一个不必要的步骤,但我在想,这可能是一种扫描代码的方法,以寻找该函数由按钮触发的提示。

您需要

func askQuestion(_ action:UIAlertAction){} // ->() is the void return here 
必需的/not
UIAlertAction
处理程序具有
UIAlertAction->()
回调,因此您必须实现,有时确实需要

alert.addAction(UIAlertAction.......{ (action) in }
因此,了解当前操作的一些详细信息(如标题或样式)非常有用

func askQuestion(_ action:UIAlertAction){} // ->() is the void return here 
必需的/not
UIAlertAction
处理程序具有
UIAlertAction->()
回调,因此您必须实现,有时确实需要

alert.addAction(UIAlertAction.......{ (action) in }

因此,了解当前操作的一些详细信息(如标题或样式)非常有用。您可以使用一个处理程序来处理多个操作

var continueAction: UIAlertAction!
var cancelAction: UIAlertAction!

override func viewDidLoad() {
    super.viewDidLoad()

    continueAction = UIAlertAction(title: "Continue", style: .default, handler: masterHandler)
    cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: masterHandler)
}

//...

 let ac = UIAlertController(title: title, message: "You tapped the button.", preferredStyle: .alert)
 ac.addAction(continueAction)
 ac.addAction(cancelAction)
就个人而言,我不知道您为什么会这样做,但API设计师认为,为您提供设计最能满足您需求的解决方案的灵活性是一个好主意


因此,在大多数情况下,虽然我理解这看起来很奇怪(特别是当您可以使用闭包时),但我确实很欣赏有这些信息来做出自己的选择

您可以让一个处理程序负责处理多个操作

var continueAction: UIAlertAction!
var cancelAction: UIAlertAction!

override func viewDidLoad() {
    super.viewDidLoad()

    continueAction = UIAlertAction(title: "Continue", style: .default, handler: masterHandler)
    cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: masterHandler)
}

//...

 let ac = UIAlertController(title: title, message: "You tapped the button.", preferredStyle: .alert)
 ac.addAction(continueAction)
 ac.addAction(cancelAction)
就个人而言,我不知道您为什么会这样做,但API设计师认为,为您提供设计最能满足您需求的解决方案的灵活性是一个好主意


因此,在大多数情况下,虽然我理解它看起来很奇怪(特别是当您可以使用闭包时),但我确实很欣赏有信息可供我自己选择

如果警报中有一个文本字段,您需要获取该文本怎么办

如果多个警报共享同一功能,并且您需要知道这是哪个警报(完全可能),该怎么办

如果这是一个UIAlertController子类,具有需要访问的属性或方法,该怎么办?(是的,我做过。)

您需要警报的引用!你需要它作为一个已经很弱的引用,这样如果你使用一个尾随闭包,你就不会得到一个retain循环。因此,动作处理程序被指定为一个参数


相信我,如果你没有得到警报的参考,迟早你会发现自己处于一种愤怒和受阻的境地

如果警报中有一个文本字段,而您需要获取该文本,该怎么办

如果多个警报共享同一功能,并且您需要知道这是哪个警报(完全可能),该怎么办

如果这是一个UIAlertController子类,具有需要访问的属性或方法,该怎么办?(是的,我做过。)

您需要警报的引用!你需要它作为一个已经很弱的引用,这样如果你使用一个尾随闭包,你就不会得到一个retain循环。因此,动作处理程序被指定为一个参数


相信我,如果你没有得到警报的参考,迟早你会发现自己处于一种愤怒和受阻的境地

您可以对多个操作使用单个处理程序,因此了解哪个操作实际调用您的handler@MadProgrammer谢谢这实际上回答了我的核心问题,但由于这是一个评论,我不能接受它作为最佳答案。您可以使用一个处理程序执行多个操作-因此了解哪个操作实际调用您的handler@MadProgrammer谢谢这实际上回答了我的核心问题,但由于这是一个评论,我不能接受它作为最佳答案。或者干脆
{uin}
@LeoDabus当然,但op认为它有用。简单地
{uin}
@LeoDabus当然,但op认为它没用