swift中UIActionSheet的使用

// 方法1
let alertSheet = UIActionSheet(title: alertTitle, delegate: nil, cancelButtonTitle: alertOK, destructiveButtonTitle: alertCancel)
alertSheet.showInView(self.view)
11.png
// 方法2
// 实例化时添加代理对象,同时注意添加协议
let alertSheet = UIActionSheet(title: alertTitle, delegate: self, cancelButtonTitle: alertOK, destructiveButtonTitle: alertCancel, otherButtonTitles: "警告", "提示", "通告")
alertSheet.showInView(self.view)

// 添加协议
class ViewController: UIViewController, UIActionSheetDelegate {
    
    override func viewDidLoad() {
        ...
    }
    ...
}
// 代理方法
// MARK: UIActionSheetDelegate
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        let buttonTitle = actionSheet.buttonTitleAtIndex(buttonIndex)
        if buttonTitle == alertCancel
        {
            print("你点击了退出")
        }
        else if buttonTitle == alertOK
        {
            print("你点击了确定")
        }
        else
        {
            print("你点击了其他")
        }
}
22.png
// 方法3
// 1 实例化
let alertSheet = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.ActionSheet)
// 2 命令(样式:退出Cancel,警告Destructive-按钮标题为红色,默认Default)
let cancelAction = UIAlertAction(title: alertCancel, style: UIAlertActionStyle.Cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)
let archiveAction = UIAlertAction(title: alertOK, style: UIAlertActionStyle.Default, handler: {
            action in
            print("OK")
})
alertSheet.addAction(cancelAction)
alertSheet.addAction(deleteAction)
alertSheet.addAction(archiveAction)
// 3 跳转
self.presentViewController(alertSheet, animated: true, completion: nil)
33.png

Swift - 操作表(UIActionSheet)的用法,也叫底部警告框:

2015041514394452781.png

1,下面创建一个操作表(或叫底部警告框)并弹出显示

import UIKit

class ViewController: UIViewController,UIActionSheetDelegate
 {
override func viewDidLoad() {
super.viewDidLoad()

 let actionSheet=UIActionSheet()

//actionSheet.title = "请选择操作"
 
actionSheet.addButtonWithTitle("取消")
actionSheet.addButtonWithTitle("动作1")       
actionSheet.addButtonWithTitle("动作2")  
actionSheet.cancelButtonIndex=0     
actionSheet.delegate=self 
actionSheet.showInView(self.view);    
}

func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {     
print("点击了:"+actionSheet.buttonTitleAtIndex(buttonIndex)!)
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,426评论 4 61
  • 父母有时也会因为某些事情,无法全部爱你,无法在时时刻刻关注到你。只有自己最清楚自己什么时候特别需要爱,需要抚慰,需...
    破壳中阅读 568评论 0 0
  • 人生中处处都是选择,如何做选择就成为一下非常重要的能力。这篇文章,我想谈谈自己对于选择的一个认识——到底有几个选择...
    安定的猫阅读 837评论 1 3
  • 我也不知道该说些什么 今天是高考结束的日子 我不敢问你 考得好不好 我怕听到否定的答案 也怕 消息石沉大海 我们分...
    顾冄阅读 278评论 0 0

友情链接更多精彩内容