Writing handler for UIAlertAction

后端 未结 9 892
北恋
北恋 2020-11-28 05:10

I\'m presenting a UIAlertView to the user and I can\'t figure out how to write the handler. This is my attempt:

let alert = UIAlertController(ti         


        
9条回答
  •  自闭症患者
    2020-11-28 05:28

    Lets assume that you want an UIAlertAction with main title, two actions (save and discard) and cancel button:

    let actionSheetController = UIAlertController (title: "My Action Title", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
    
        //Add Cancel-Action
        actionSheetController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    
        //Add Save-Action
        actionSheetController.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
            print("handle Save action...")
        }))
    
        //Add Discard-Action
        actionSheetController.addAction(UIAlertAction(title: "Discard", style: UIAlertActionStyle.Default, handler: { (actionSheetController) -> Void in
            print("handle Discard action ...")
        }))
    
        //present actionSheetController
        presentViewController(actionSheetController, animated: true, completion: nil)
    

    This works for swift 2 (Xcode Version 7.0 beta 3)

提交回复
热议问题