9. UIActionSheet

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的定義,我們發現, 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.
    }

}
發佈了36 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章