我想在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之后返回某项内容时,它首先返回该值,然后出现ui警报。是否有一种方法可以先发出警告,然后返回变量?
发布于 2015-08-03 17:44:13
如果您只是试图更新allergies中的值,则可以保留对要更新的属性(self.allergies)的引用,然后可以在操作中增加该属性。
如果需要返回此值,可以在处理程序中调用第二个方法:
severe.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
self.returnAllergiesWithVal(2);
}))然后,returnAllergiesWithVal方法可以将该值传递到需要它的任何地方。
https://stackoverflow.com/questions/31793030
复制相似问题