gpt4 book ai didi

ios - @IBAction 递归调用

转载 作者:行者123 更新时间:2023-11-29 01:55:47 26 4
gpt4 key购买 nike

我只是想就递归调用 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

....

}

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

最佳答案

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

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

关于ios - @IBAction 递归调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com