如何创建 UIActionSheet 操作?

How to create UIActionSheet actions?

我知道我必须设置委托(我是这样设置的:class ShiftOverview: UITableViewController, UIActionSheetDelegate {...)但是当我点击按钮时仍然没有任何反应。

看起来您也不能使用 UIAlertController 在函数中设置委托...

 @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")

    }
}

在 iOS8 之前,UIAlertViews 和 UIActionSheets 是独立的控件。然而,在 iOS 8 中,新的 class、UIAlertController 将这两个控件组合成一个易于使用的单一控件 class.

您现在传递要调用的闭包,而不是像这些控件过去那样使用委托模式。 handler 等于 nil 的地方就是放置代码的地方。

这可能是添加的,因为 Swift 将关闭视为第一个 class 公民,而 Objective C 没有那么多(有块)。

应该是:

@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)
}

NSHipster's article on UIAlertControllers or in its documentation 中阅读更多相关信息。

您可以编写要执行的操作,而不是将 nil 传递给您的处理程序。您不再需要委托方法。

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")
    };

您可能还想看这个:

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
        }
    }