In my app I'm creating, I have an ActionSheet with some options. When the user selects an option, I want the label to change to what he/she chose. I tried an if/else statement, but I obviously did not create it correctly-
@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()
}
Question- How do I apply an if/else statement to the user's option? Thanks!
Update your UIAlertAction handler to include the code for changing the label.
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)"
})