如何创建UIActionSheet操作?

时间:2015-01-05 20:57:45

标签: ios swift uiactionsheet

我知道我必须设置委托(我这样做: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")

    }
}

3 个答案:

答案 0 :(得分:0)

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

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

这可能是因为Swift将封闭视为一等公民,而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或其documentation中了解详情。

答案 1 :(得分:0)

您可以编写要执行的操作,而不是将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")
    };

答案 2 :(得分:0)

您可能也希望看到这一点:

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