UIAlertAction 中的 return int 或 UIAlertAction 之后的 return int

return int in UIAlertAction or return int after UIAlertAction

我想 return UIAlertAction 中的一个整数。 现在我收到错误 'Int is not convertible to Void.' The function the alert is supposed to return int.

这是我的代码:

func writeSteps() -> Int {
    var allergies = 0

    var severe = UIAlertController(title: "More information", message: "blablabla", preferredStyle: UIAlertControllerStyle.Alert)

    severe.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in

         allergies += 1
         return allergies


    }))

    severe.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
         allergies += 2
         return allergies

    }))
    self.presentViewController(severe, animated: true, completion: nil)
}

这可能吗?

如果不是,每当我尝试 return uiAlertController 之后的内容时,它首先 returns,然后 uialert 出现。有没有办法让 uialert 先走,然后 return 一个变量?

如果您只是想更新 allergies 中的值,您可以保留对您尝试更新的 属性 (self.allergies) 的引用,然后您可以递增在你的行动中。

如果您需要 return 这个值,您可以在处理程序中调用第二个方法:

severe.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
     self.returnAllergiesWithVal(2);
}))

然后 returnAllergiesWithVal 方法可以将该值传递到您需要的任何地方。