-1

I'm trying to add an action sheet. From what I have read, UIActionSheet is depreciated in IOS 8, and one should use an Alert Controller instead. Here is my code snippet:

let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let takePhoto = UIAlertAction(title: "Take Photo", style: .Default, handler: { (alert: UIAlertAction) -> Void in
//action goes here 
})

However. I am getting an error which reads "Could not find member 'Default'". From what I understand it there are 3 styles possible for UIAlertAction: Default, Cancel & Destructive. What am I doing wrong?

Many thanks :)

dan martin
  • 1,307
  • 3
  • 15
  • 29

2 Answers2

0

The problem is actually with the closure, rather than with .Default, but that's what the compiler is complaining about. The type on alert is incorrect (just barely). You can change the closure definition to one of these to make it work:

... handler: { (alert: UIAlertAction!) -> Void in
... handler: { alert -> Void in
... handler: { _ -> Void in //Use this if you don't care about the alert object

Shameless plug: You could also use something like LKAlertController to make the action sheet creation really easy. It would turn into something like

ActionSheet().addAction(title: "Take Photo", style: .Default) {
    //Action
}.addAction(/* Your other actions */).show()
esthepiking
  • 1,647
  • 1
  • 16
  • 27
0

This compiles but you have more to do like add the .addAction, etc.. Note that the preferred style in optionMenu is .Alert. There is an excellent explanation in IOS 8 Swift Cookbook by Vandad Nahavandipoor

        let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
        let takePhoto = UIAlertAction(title: "Take Photo",
        style: UIAlertActionStyle.Default,
        handler: {[weak self] (paramAction:UIAlertAction!) in

        })
Syed Tariq
  • 2,878
  • 3
  • 27
  • 37