我想在UIAlertAction中返回一个整数。 现在我收到错误“Int不能转换为Void”。 警报所在的函数应该返回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) }这可能吗?
如果没有,每当我尝试在uiAlertController之后返回某个东西时,它首先返回,然后出现uialert。 有没有办法让uialert先去,然后返回一个变量?
I would like to return an integer in a UIAlertAction. Right now I get the error 'Int is not convertible to Void.' The function the alert is in is supposed to return int.
Here is my code:
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) }Is this possible?
If not, whenever I try to return something after the uiAlertController, it returns that first, and then the uialert appears. Is there a way to make the uialert go first and then return a variable?
最满意答案
如果你只是试图更新allergies的价值,你可以保留一个参考你试图更新的属性(self.allergies),然后你可以在你的行为中增加。
如果需要返回此值,可以在处理程序中调用第二种方法:
severe.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in self.returnAllergiesWithVal(2); }))然后returnAllergiesWithVal方法可以将该值传递到任何需要的地方。
If you are simply trying to update the value in allergies you could keep a reference to the property you're trying to update (self.allergies) which you can then increment in your actions.
If you need to return this value, you could call a second method within your handlers:
severe.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in self.returnAllergiesWithVal(2); }))Where the returnAllergiesWithVal method can then pass that value on to wherever you need it.
更多推荐
发布评论