未经调试的 ActionSheet 选择器。。。

2019-11-07  本文已影响0人  红色海_

未经调试的 ActionSheet 选择器。。。

//
//  MySelector.swift
//  TestMyActionSheet
//
//  Created by King on 2019/11/7.
//  Copyright © 2019 King. All rights reserved.
//

import UIKit


/********使用方法举例***********************
let titles = ["111", "222"]
var selector = MySelector.init(self,
                       clickedItem,
                               "Please select",
                               titles, { (index:Int)->Void in
                                if titles[index] == "111" {
                                    //
                                }}
).showFromBarItem()
********************************************/

// tag属性
class MyAlertAction: UIAlertAction {
    var tag : Int?
}

// 点击相应的闭包
typealias MyOption = ( _:Int)->Void

class MySelector: NSObject {
    
    var alert : UIAlertController
    var viewController : UIViewController
    
    var tableView : UITableView?
    var indexPath : IndexPath?
    
    var barBtnItem : UIBarButtonItem?
    
    var selectorTitles : Array<String>!
    var selectedOption:MyOption!
    
    //构造函数 for UITableViewCell 弹出
    init(_ vc:UIViewController,
         _ tv:UITableView,
         _ path:IndexPath,
         _ title:String,
         _ titles:Array<String>,
         _ option:@escaping MyOption) {
        viewController = vc
        selectorTitles = titles
        tableView = tv
        indexPath = path
        selectorTitles = titles
        selectedOption = option
        alert = UIAlertController.init(title: title, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    }
    
    //构造函数 for UIBarButtonItem 弹出
    init(_ vc:UIViewController,
         _ item:UIBarButtonItem,
         _ title:String,
         _ titles:Array<String>,
         _ option:@escaping MyOption) {
        viewController = vc
        selectorTitles = titles
        barBtnItem = item
        alert = UIAlertController.init(title: title, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    }
    
    // 添加选项。。
    func addActions() -> Void {
        for i in 0...selectorTitles.count {
            
            let action = MyAlertAction.init(title: selectorTitles[i], style: .default,
                                            handler: {(alertAction) in
                                                let action = alertAction as! MyAlertAction
                                                self.selectedOption(action.tag!)})
            action.tag = i
            alert.addAction(action)
        }
    }
    
   // UITableViewCell 弹出
    func showFromTableViewCell() ->Void {
        
        addActions()
        
        let popover : UIPopoverPresentationController? = alert.popoverPresentationController
        if nil != popover {
            popover?.sourceView = tableView
            popover?.sourceRect = (tableView?.cellForRow(at: indexPath!)!.frame)!
            popover?.permittedArrowDirections = .any
        }
        viewController.present(alert, animated: true, completion: nil)
    }
    
    // UIBarButtonItem弹出
    func showFromBarItem() -> Void {
        
        addActions()
        
        let popover : UIPopoverPresentationController? = alert.popoverPresentationController
        if nil != popover {
            popover?.sourceView = tableView
            popover?.barButtonItem = barBtnItem!
        }
        viewController.present(alert, animated: true, completion: nil)
    }
}
上一篇 下一篇

猜你喜欢

热点阅读