如何在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中添加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+

    //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)

Ios相关问答推荐

如何调整包含extView的UICollectionViewListCell的大小?

实例方法wait在异步上下文中不可用;请改用TaskGroup;这是Swift 6中的错误''

设置堆栈视图的所有属性

如何在SwiftUI中扩展双击的可检测区域?

从后台线程调用DispatchObject的方法安全吗?

如何在SwiftUI中顺时针和逆时针两个方向应用旋转效果?

如何在 SwiftUI 中通过通用 @ObservedObject 进行切换?

如何解码没有名称的 JSON 数组?

卡在 Apple 的 SwiftUI 教程第 5 章(更新应用数据)

SwiftUI 中的偏移量和按钮有问题吗?

JSONDecoder 在模拟器上运行良好时无法在真实设备上解码

Swift tableView 分页

在 Swift 中追加字符串

从 NSUserDefaults 字典 iOS 中删除所有键

Xcode / iOS模拟器:手动触发重大位置更改

iOS 7 半透明模态视图控制器

单击 GoogleSignIn 按钮时应用程序崩溃

如何删除 Swift 数组中的所有 nil 元素?

使用 Xcode 的 All Exceptions 断点时忽略某些异常

在 iOS 上没有显示数字键盘