在我正在创建的应用程序中,我有一个带有一些选项的ActionSheet。当用户选择一个选项时,我希望标签更改为他/她选择的内容。我尝试了一个if / else语句,但我显然没有正确创建它 -
@IBAction func showActionSheet(sender: AnyObject) {
var optionMenu = UIAlertController(title: nil, message: "Choose a Factor", preferredStyle: .ActionSheet)
let factorActionCageRest = UIAlertAction(title: "Cage Rest", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("File Deleted")
})
let factorActionAfterSurgery = UIAlertAction(title: "After Surgery", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("File Saved")
})
let factorActionTrauma = UIAlertAction(title: "Trauma", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("File Saved")
})
let factorActionSepsis = UIAlertAction(title: "Sepsis", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("File Saved")
})
let factorActionSevereBurn = UIAlertAction(title: "Severe Burn", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("File Saved")
})
let factorCancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(alert: UIAlertAction!) -> Void in
print("Cancelled")
})
optionMenu.addAction(factorActionCageRest)
optionMenu.addAction(factorActionAfterSurgery)
optionMenu.addAction(factorActionTrauma)
optionMenu.addAction(factorActionSepsis)
optionMenu.addAction(factorActionSevereBurn)
optionMenu.addAction(factorCancelAction)
if optionMenu.addAction() = factorActionCageRest {
var factorActionOutput = 1.25
factorLabel.text = "\(factorActionOutput)"
}
self.presentViewController(optionMenu, animated: true, completion: nil)
}
@IBOutlet weak var factorLabel: UILabel!
var factorActionOutput = Float()
}
问题 - 如何将if / else语句应用于用户的选项?谢谢!
答案 0 :(得分:0)
当使用按钮时,会运行该操作的handler
闭包。如果你想要发生某些事情,你需要在处理器内部添加代码,以便更好地实现它。
例如,按下此按钮时,您可以更改第一个操作以更改factorLabel
的文本。
let factorActionCageRest = UIAlertAction(title: "Cage Rest", style: .Default, handler: { (alert: UIAlertAction!) -> Void in
self.factorLabel.text = "File Deleted"
})
答案 1 :(得分:0)
我知道您希望在用户选择选项时更改标签和变量。因此,您可以使用alertAction
来更新标签
let factorActionCageRest = UIAlertAction(title: "Cage Rest", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
print("File Deleted")
// Change the label and variable here
})
答案 2 :(得分:0)
更新您的UIAlertAction处理程序以包含更改标签的代码。
let factorActionCageRest = UIAlertAction(title: "Cage Rest", style: .Default, handler: {
[weak self] (alert: UIAlertAction!) -> Void in
print("File Deleted")
let factorActionOutput = 1.25
self?.factorLabel.text = "\(factorActionOutput)"
})