UIActionSheet
UIActionSheet和UIAlertView非常类似, 我们将学习如何使用它
1.UIActionSheet创建
import UIKit
class 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 UIKit
class 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.
}
}