@IBAction递归调用

时间:2015-06-16 21:06:42

标签: ios iphone swift uialertaction

我只是希望得到第二个意见,如果有任何无法预料的问题,递归调用IBAction函数。我问这个问题是因为发生了一次崩溃,我发现很难复制。

假设我们有这个电话:

s.getPhones().addAll(
    Arrays.asList("123,456,789".split(","))
        .stream()
        .map(Long::valueOf)  // You could use: (s -> Long.valueOf(s))
        .collect(Collectors.toList()));

这是在Swift中递归调用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 .... } 的正确方法吗?

1 个答案:

答案 0 :(得分:1)

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

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

相关问题