在UIAlertAction中返回int或在UIAlertAction后返回int(return int in UIAlertAction or return int after UIAlertAct

系统教程 行业动态 更新时间:2024-06-14 16:55:58
在UIAlertAction中返回int或在UIAlertAction后返回int(return int in UIAlertAction or return int after UIAlertAction)

我想在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.

更多推荐

本文发布于:2023-04-10 11:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/63717324f9b25f77cab6e7d335f51d8c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIAlertAction   int   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!