UIActionSheet iOS Swift

编程入门 行业动态 更新时间:2024-10-26 12:24:03
本文介绍了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.

您可以添加 UIActionSheetDelegate 在UIViewController中像

You can add UIActionSheetDelegate in UIViewController` like

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

单击时可以获得按钮索引

You can get your button index when it clicked like

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

本文发布于:2023-08-05 17:05:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1306843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIActionSheet   iOS   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!