Search code examples
iosswiftuiactionsheetios8uiactionsheetdelegate

Handling events on UIActionSheet in SWIFT


I am working on creating UIActionSheet for iOS8 in SWIFT. I have a working code, but I am stuck with the handling of button clicks for the action sheet.

My current code is:

    var actionSheet =  UIAlertController(title: "Publish this Image ?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    actionSheet.addAction(UIAlertAction(title: "Tweet", style: UIAlertActionStyle.Cancel, handler: nil))
    actionSheet.addAction(UIAlertAction(title: "WhatsApp", style: UIAlertActionStyle.Default, handler: nil))
    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Destructive, handler: nil))
    self.presentViewController(actionSheet, animated: true, completion: nil)

How do I handle the button click events ?? Do I need to use the handle parameter in any way ??


Solution

  • You could try something like:

        var actionSheet =  UIAlertController(title: "Publish this Image ?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        actionSheet.addAction(UIAlertAction(title: "Tweet", style: UIAlertActionStyle.Default, handler: { (ACTION :UIAlertAction!)in
            //your code here...
            }))
        actionSheet.addAction(UIAlertAction(title: "WhatsApp", style: UIAlertActionStyle.Default, handler: { (ACTION :UIAlertAction!)in
            //your code here...
        }))
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
        self.presentViewController(actionSheet, animated: true, completion: nil)