如何呈现UIActionSheet iOS Swift? [英] How to present UIActionSheet iOS Swift?

查看:68
本文介绍了如何呈现UIActionSheet iOS Swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS Swift中执行UIActionSheet? 这是我用于编码UIActionSheet的代码.

How To Do UIActionSheet in iOS Swift? Here is my code for coding 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)
}

我希望我的代码清晰明了... 欢迎对此代码提出更好的建议.

I hope my Code is clear... I am welcome better suggestion for this code.

推荐答案

您的方法很好,但是您可以轻松地以其他方式添加UIActionSheet.

Your Approach is fine, but you can add UIActionSheet with other way with ease.

您可以像

那样在UIViewController中添加UIActionSheetDelegate

class ViewController: UIViewController ,UIActionSheetDelegate

将您的方法设置为

@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 +

Update 1: for 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(title: "Cancel", style: .cancel) { _ in
        print("Cancel")
    }
    actionSheetControllerIOS8.addAction(cancelActionButton)

    let saveActionButton = UIAlertAction(title: "Save", style: .default)
        { _ in
           print("Save")
    }
    actionSheetControllerIOS8.addAction(saveActionButton)

    let deleteActionButton = UIAlertAction(title: "Delete", style: .default)
        { _ in
            print("Delete")
    }
    actionSheetControllerIOS8.addAction(deleteActionButton)
    self.present(actionSheetControllerIOS8, animated: true, completion: nil)

这篇关于如何呈现UIActionSheet iOS Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆