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")
}
}
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 tonil
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:
Read more about this in NSHipster's article on UIAlertControllers or in its documentation.
You may like to see this also:
Instead of passing nil to your handler, you can write the action you want to perform. You don't need the delegate method anymore.