0

我只是想就递归调用 IBAction 函数是否有任何不可预见的问题获得第二意见。我问这个问题是因为曾经发生过一次崩溃,我发现它很难复制。

假设我们有这个电话:

var 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) // call self again
            }))  

     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

    ....    

}   

这是@IBAction在 Swift 中递归调用的正确方法吗?

4

1 回答 1

1

是的,这是递归调用函数的正确方法,就像使用任何其他函数一样。

注意:
不管它们的类型如何,创建这么多 UIController 都不是一个好习惯。
此外,由于您的递归并非旨在结束,这将导致无限数量的调用,并可能导致程序因堆栈溢出而崩溃——我不建议这样做。

于 2015-06-16T21:25:21.213 回答