9. UIActionSheet
来源:互联网 发布:小说改编的网络剧2015 编辑:程序博客网 时间:2024/06/05 20:16
UIActionSheet
UIActionSheet和UIAlertView非常类似, 我们将学习如何使用它
1.UIActionSheet创建
import UIKitclass ViewController: UIViewController, UIActionSheetDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let actionSheet = UIActionSheet() actionSheet.title = "烦恼" actionSheet.addButtonWithTitle("钱解决") actionSheet.addButtonWithTitle("更多钱解决") actionSheet.addButtonWithTitle("取消") actionSheet.cancelButtonIndex = 2 actionSheet.delegate = self actionSheet.showInView(self.view) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
运行程序:
注意:
查看UIActionSheet的定义,我们发现, UIActionSheet的显示方式有多种,如在UIToolbar上显示,在UITabBar上显示, 在UIBarButtonItem上显示,和指定的UIView上显示,具体看下面的方法:
public func showFromToolbar(view: UIToolbar)public func showFromTabBar(view: UITabBar)@available(iOS 3.2, *)public func showFromBarButtonItem(item: UIBarButtonItem, animated: Bool)@available(iOS 3.2, *)public func showFromRect(rect: CGRect, inView view: UIView, animated: Bool)public func showInView(view: UIView)
设置标题看起来好low,还不如没有!
2. UIActionSheetDelegate
查看一个UIActionSheetDelegate的定义
public protocol UIActionSheetDelegate : NSObjectProtocol { // 有按钮被点击的时候调用 @available(iOS, introduced=2.0, deprecated=8.3) optional public func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) // 操作表显示时,应用突然关闭时调用(如用户俺下了home键) // 注意,按下cancle是不会调用此方法的 @available(iOS, introduced=2.0, deprecated=8.3) optional public func actionSheetCancel(actionSheet: UIActionSheet) @available(iOS, introduced=2.0, deprecated=8.3) optional public func willPresentActionSheet(actionSheet: UIActionSheet) // 显示前被调用 @available(iOS, introduced=2.0, deprecated=8.3) optional public func didPresentActionSheet(actionSheet: UIActionSheet) // 显示后被调用 @available(iOS, introduced=2.0, deprecated=8.3) optional public func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) // 隐藏前被调用 @available(iOS, introduced=2.0, deprecated=8.3) optional public func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) // 隐藏后被调用}
现在我们实现一个需求:
我们点击了哪个按钮, 就在控制台打印出它对应的索引和标题
// MARK: - UIActionSheetDelegate func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { print("click index: \(buttonIndex), title is \(actionSheet.buttonTitleAtIndex(buttonIndex))") }
运行程序,每个按钮都点击一下,可以在控制台中看见如下结果:
click index: 0, title is Optional("钱解决")click index: 1, title is Optional("更多钱解决")click index: 2, title is Optional("取消")
3. 完整代码:
import UIKitclass ViewController: UIViewController, UIActionSheetDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let actionSheet = UIActionSheet() actionSheet.title = "烦恼" actionSheet.addButtonWithTitle("钱解决") actionSheet.addButtonWithTitle("更多钱解决") actionSheet.addButtonWithTitle("取消") actionSheet.cancelButtonIndex = 2 actionSheet.delegate = self actionSheet.showInView(self.view) } // MARK: - UIActionSheetDelegate func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { print("click index: \(buttonIndex), title is \(actionSheet.buttonTitleAtIndex(buttonIndex))") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
2 0
- 9. UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet
- UIActionSheet 用法
- BZOJ 3910: 火车|LCA|并查集
- 4281: [ONTAK2015]Związek Harcerstwa Bajtockiego 倍增LCA
- HBase超详细介绍
- ubuntu14.04安装与配置cloudmonkey
- Cocoa Touch事件处理流程--响应者链
- 9. UIActionSheet
- iOS 自定制搜索栏,保存历史搜索
- 基于uC/OSII多任务的STM32报警模块
- Where is the xm Command in XenServer
- 细说Java
- 循序渐进linux-笔记
- C语言学习笔记2-变量
- ant用法;
- linux笔记