当我递归调用@IBAction 时发生了这个崩溃
0 Goga 0x00000001000b90b8 function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Owned To Guaranteed> of Goga.NewViewController.emailButtonPressed (Goga.NewViewController)(ObjectiveC.UIButton) -> () (NewViewController.swift:0)
1 Goga 0x00000001000c0488 Goga.NewViewController.(emailButtonPressed (Goga.NewViewController) -> (ObjectiveC.UIButton) -> ()).(closure #2) (NewViewController.swift:872)
2 Goga 0x00000001000bd250 partial apply forwarder for reabstraction thunk helper from @callee_owned (@in ObjectiveC.UIAlertAction!) -> (@out ()) to @callee_owned (@owned ObjectiveC.UIAlertAction!) -> (@unowned ()) (NewViewController.swift:0)
这是代码和其中发生的行
allowedToSend = false
@IBAction func emailButtonPressed(sender: UIButton) {
if !allowedToSend {
let controller = UIAlertController(title: title,
message: "Are you sure you want to send the Email?",
preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "Yes please, send.", style: .Default, handler: {
action in
self.allowedToSend = true;
self.emailButtonPressed(sender) <=== WHERE THE CRASH HAPPENED
}))
controller.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
presentViewController(controller, animated: true, completion: nil)
return // exit the function
}
// Reset the value
allowedToSend = false
// Sending code
let text = textArea.text
let to = toLabel.text
....
}
您可能会问,为什么我不在处理程序中放入“发送代码”,因为我这样做只是针对用户即将向其地址簿中的所有联系人进行批量发送的特定情况.因此,我提醒她,她将在发送前完成此操作。
我不确定这种实现有什么问题以及为什么 Swift 会提示这个,因为我在 Debug模式下测试过它并且它看起来不错。为什么会这样?
编辑
如果我能深入了解崩溃的含义,那就太好了。
最佳答案
为了可读性和可维护性,我建议拆分函数。这也将解决任何递归问题并消除对 allowedToSend
属性的需要。无论如何,由于简化了执行路径,调试起来会更容易。
@IBAction func emailButtonPressed(sender: UIButton) {
self.confirm();
}
func confirm() {
let controller = UIAlertController(title: title,
message: "Are you sure you want to send the Email?",
preferredStyle: .Alert)
controller.addAction(UIAlertAction(title: "Yes please, send.", style: .Default, handler: {
action in
self.send()
}))
controller.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
presentViewController(controller, animated: true, completion: nil)
}
func send() {
let text = textArea.text
let to = toLabel.text
....
}
关于ios - @IBAction 在递归调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31003351/