UIKit框架-高階控制元件Swift版本: 7.UIActionSheet方法/屬性詳解
阿新 • • 發佈:2018-12-30
前面我們學習了 iOS 中得第一個提示性控制元件 UIAlertView, 現在我們把第二個提示性控制元件也學完.
1.UIActionSheet常用屬性
// 1.設定 UIActionSheet 的代理物件
var delegate: UIActionSheetDelegate?
// 2.設定 UIActionSheet 的標題
var title: String
// 3.設定 UIActionSheet 的樣式
var actionSheetStyle: UIActionSheetStyle
// 4.讀取 UIActionSheet 裡有多少個按鈕
var numberOfButtons: Int { get }
// 5.設定 UIActionSheet 取消按鈕的索引
var cancelButtonIndex: Int
// 6.設定其他按鈕的索引
var destructiveButtonIndex: Int
// 7.讀取 UIActionSheet 其他按鈕的索引值
var firstOtherButtonIndex: Int { get }
// 8.讀取 UIActionSheet 是否可見
var visible: Bool { get }
2.UIActionSheet的常用方法
// 1.該方法是用來新增 UIActionSheet 的按鈕標題
func addButtonWithTitle(title: String) -> Int
// 2.該方法是用來設定 UIActionSheet 的按鈕索引
func buttonTitleAtIndex(buttonIndex: Int) -> String
// 3.該方法是來用設定 UIActionSheet 顯示到 ToolBar
func showFromToolbar(view: UIToolbar!)
// 4.該方法是用來設定 UIActionSheet 顯示到 TabBar
func showFromTabBar(view: UITabBar!)
// 5.該方法是用來設定來自 UIBarButtonItem 的 UIActionSheet, 並且是否開啟動畫效果
func showFromBarButtonItem(item: UIBarButtonItem!, animated: Bool)
// 6.該方法是用來設定 UIActionSheet 的顯示的檢視大小, 以及指定檢視和是否開啟動畫效果
func showFromRect(rect: CGRect, inView view: UIView!, animated: Bool)
// 7.該方法是用來設定 UIActionSheet 顯示到哪一個檢視
func showInView(view: UIView!)
// 8.該方法是用來設定 UIActionSheet 消失的按鈕索引, 以及是否使用動畫
func dismissWithClickedButtonIndex(buttonIndex: Int, animated: Bool)
3.UIActionSheet的代理方法
// 1.該方法是在 UIActionSheet 上的按鈕被點選時呼叫的
optional func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
// 2.該方法是在 UIActionSheet 上的點選了取消按鈕時呼叫的
optional func actionSheetCancel(actionSheet: UIActionSheet)
// 3.該方法是在 UIActionSheet 完全即將顯示的時候呼叫的
optional func willPresentActionSheet(actionSheet: UIActionSheet)
// 4.該方法是在 UIActionSheet 完全顯示的時候呼叫的
optional func didPresentActionSheet(actionSheet: UIActionSheet)
// 5.該方法是在 UIActionSheet 完全即將消失的時候呼叫的
optional func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int)
// 6.該方法是在 UIActionSheet 完全消失的時候呼叫的
optional func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int)
3.程式碼演示
首先遵守代理協議
class ViewController: UIViewController, UIActionSheetDelegate {}
自定義UIButton並且監聽 ActionSheet 方法
func myButton() {
var button: UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(100, 200, 50, 20)
button.setTitle("彈窗", forState: UIControlState.Normal)
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "myActionSheet", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
自定義UIActionSheet
func myActionSheet() {
// 1.自定義 UIActionSheet, 並且設定標題, 代理物件, 以及按鈕的標題
var actionSheet = UIActionSheet(title: "UIActionSheet", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "按鈕一")
// 2.設定 UIActionSheet 的樣式
actionSheet.actionSheetStyle = UIActionSheetStyle.Default
// 3.設定取消按鈕的索引
actionSheet.cancelButtonIndex = 1
// 4.設定destructive的索引值
actionSheet.destructiveButtonIndex = 0
// 5.新增其他按鈕的標題
actionSheet.addButtonWithTitle("按鈕二")
// 6.設定按鈕標題的索引
actionSheet.buttonTitleAtIndex(1)
// 7.顯示到 self.view 上
actionSheet.showInView(self.view)
}
實現UIActionSheet代理方法
// 1.該方法是在 UIActionSheet 上的按鈕被點選時呼叫的
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
println("被點選了")
}
// 2.該方法是在 UIActionSheet 上的點選了取消按鈕時呼叫的
func actionSheetCancel(actionSheet: UIActionSheet) {
println("點選了取消按鈕")
}
// 3.該方法是在 UIActionSheet 完全即將顯示的時候呼叫的
func willPresentActionSheet(actionSheet: UIActionSheet) {
println("UIActionSheet即將顯示")
}
// 4.該方法是在 UIActionSheet 完全顯示的時候呼叫的
func didPresentActionSheet(actionSheet: UIActionSheet) {
println("UIActionSheet完全顯示")
}
// 5.該方法是在 UIActionSheet 完全即將消失的時候呼叫的
func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
println("UIActionSheet即將消失")
}
// 6.該方法是在 UIActionSheet 完全消失的時候呼叫的
func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
println("UIActionSheet完全消失")
}
在viewDidLoad實現
override func viewDidLoad() {
super.viewDidLoad()
self.myButton()
}
4.最終效果
PS: UIActionSheet 是繼承於UIView 的,所以 UIView 裡的方法和屬性 UIActionSheet 都是可以使用的.
好了, 這次我們就講到這裡, 下次我們繼續