advantis
3/13/2015 - 12:14 PM

ActionSheet.swift

import UIKit

typealias SheetAction = () -> Void

class ActionSheet: UIActionSheet {
    private var actions = [SheetAction]()

    convenience init() {
        self.init(frame: CGRectZero)
        delegate = self
    }

    override func addButtonWithTitle(title: String) -> Int {
        return addButtonWithTitle(title) {}
    }

    func addButtonWithTitle(title: String, action: SheetAction) -> Int {
        let index = super.addButtonWithTitle(title)
        actions.insert(action, atIndex: index)
        return index
    }
}

extension ActionSheet: UIActionSheetDelegate {
    func actionSheet(_: UIActionSheet, clickedButtonAtIndex index: Int) {
        // Check for the case of tapping outside an action sheet on iPad
        if index < actions.count {
            actions[index]()
        }
    }
}