UIActionSheet iOS Swift

如何在iOS Swift中做UIActionSheet?
这里是我的代码UIActionSheet。

@IBAction func downloadSheet(sender: AnyObject)
{
    let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet) 

    let saveAction = UIAlertAction(title: "Save", style: .Default, handler: 
    {
        (alert: UIAlertAction!) -> Void in
        println("Saved")
    })

    let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler:
    {
        (alert: UIAlertAction!) -> Void in
        println("Deleted")
    })

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: 
    {
        (alert: UIAlertAction!) -> Void in
        println("Cancelled")
    })
    optionMenu.addAction(deleteAction)
    optionMenu.addAction(saveAction)
    optionMenu.addAction(cancelAction)
    self.presentViewController(optionMenu, animated: true, completion: nil)
}

我希望我的代码清晰…
我欢迎更好的建议这个代码。

你的方法很好,但你可以用其他方式轻松添加UIActionSheet。

你可以在UIViewController` like中添加UIActionSheetDelegate

class ViewController: UIViewController ,UIActionSheetDelegate

设置你的方法like,

@IBAction func downloadSheet(sender: AnyObject)
{

    let actionSheet = UIActionSheet(title: "Choose Option", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Save", "Delete")

    actionSheet.showInView(self.view)
}

你可以得到你的按钮索引当它点击像

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
{
    println("\(buttonIndex)")
    switch (buttonIndex){

    case 0:
        println("Cancel")
    case 1:
        println("Save")
    case 2:
        println("Delete")
    default:
        println("Default")
        //Some code here..

    }
}

更新1:适用于iOS8

    //Create the AlertController and add Its action like button in Actionsheet
    let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .ActionSheet)

    let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
        print("Cancel")
    }
    actionSheetControllerIOS8.addAction(cancelActionButton)

    let saveActionButton: UIAlertAction = UIAlertAction(title: "Save", style: .Default)
        { action -> Void in
           print("Save")
    }
    actionSheetControllerIOS8.addAction(saveActionButton)

    let deleteActionButton: UIAlertAction = UIAlertAction(title: "Delete", style: .Default)
        { action -> Void in
            print("Delete")
    }
    actionSheetControllerIOS8.addAction(deleteActionButton)
    self.presentViewController(actionSheetControllerIOS8, animated: true, completion: nil)
http://stackoverflow.com/questions/29887869/uiactionsheet-ios-swift

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:UIActionSheet iOS Swift