How to create UIActionSheet actions?
Asked Answered
F

3

1

I know I have to set the delegate (I did so like this: class ShiftOverview: UITableViewController, UIActionSheetDelegate {...) but I'm still getting no response when I tap the buttons.

It doesn't look like you can set the delegate within the function with UIAlertController either...

 @IBAction func takeShift(sender: AnyObject) {

    let myActionSheet = UIAlertController (title: "Confirm", message: "Test message", preferredStyle: UIAlertControllerStyle.ActionSheet)

    let actionOne = UIAlertAction (title: "Take Shift", style: .Default, handler: nil)
    let actionTwo = UIAlertAction (title: "View ESA", style: .Default, handler: nil)

    let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel, handler: nil)

    myActionSheet.addAction(actionOne)
    myActionSheet.addAction(actionTwo)
    myActionSheet.addAction(actionCancel)

    self.presentViewController(myActionSheet, animated: true, completion: nil)
}

func actionSheet (myActionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
{
    switch buttonIndex {

    case 0:
        println ("test0")
        break
    case 1:
        println ("test1")
        break
    case 2:
        println ("test2")
        break

    default:
        println("nope")

    }
}
Felspar answered 5/1, 2015 at 20:57 Comment(0)
G
0

Prior to iOS 8, UIAlertViews and UIActionSheets were separate controls. In iOS 8 however, a new class, UIAlertController, combines both these controls into a single, easy to use class.

Rather than using the delegation pattern like these controls used to, you now pass a closure to be called. The places where you have handler equal to nil is where you put your code.

This is likely added because Swift treats closures as first-class citizens, while Objective C did not as much (with blocks).

It should be:

@IBAction func takeShift(sender: AnyObject) {

    let myActionSheet = UIAlertController (title: "Confirm", message: "Test message", preferredStyle: UIAlertControllerStyle.ActionSheet)

    let actionOne = UIAlertAction (title: "Take Shift", style: .Default, handler: { (action) in
        println("test0")
    })
    let actionTwo = UIAlertAction (title: "View ESA", style: .Default, handler: { (action) in
        println("test1")
    })

    let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel, handler: { (action) in
        println("test2")
    })

    myActionSheet.addAction(actionOne)
    myActionSheet.addAction(actionTwo)
    myActionSheet.addAction(actionCancel)

    self.presentViewController(myActionSheet, animated: true, completion: nil)
}

Read more about this in NSHipster's article on UIAlertControllers or in its documentation.

Guajardo answered 5/1, 2015 at 21:7 Comment(0)
S
0

Instead of passing nil to your handler, you can write the action you want to perform. You don't need the delegate method anymore.

let actionOne = UIAlertAction (title: "Take Shift", style: .Default){ (action) -> Void in
         println ("Take Shift")
    };
let actionTwo = UIAlertAction (title: "View ESA", style: .Default){ (action) -> Void in
        println ("View ESA")
    };

let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel){ (action) -> Void in
        println ("Cancel")
    };
Schilit answered 5/1, 2015 at 21:8 Comment(0)
G
0

You may like to see this also:

func AlertViewWithActionsheet () {
        let alert = UIAlertController(title: "Alert with actionsheet", message: "Alert with actionsheet", preferredStyle: UIAlertControllerStyle.ActionSheet);

        let dismissHandler = {
            (action: UIAlertAction!) in
                // This is an action event - called when you press OK button.
                self.dismissViewControllerAnimated(true, completion: { () -> Void in
            })
        }

        // add action
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: dismissHandler))

        presentViewController(alert, animated: true) { () -> Void in
        }
    }
Guaiacol answered 6/1, 2015 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.