zoukankan      html  css  js  c++  java
  • Swift用block响应UIAlertView和UIActionSheet的按钮点击事件

    UIAlertView:

    //
    //  UIAlertView+Block.swift
    //  ECExpert
    //
    //  Created by Fran on 15/6/11.
    //  Copyright (c) 2015年 Fran. All rights reserved.
    //
    
    import Foundation
    
    /**
    *
    *   objc_setAssociatedObject 无法将 Function 传递过去
    *
    *   在这里使用 class CompleteAlertViewFuncClass 做了一层包装
    *
    */
    
    typealias CompleteAlertViewFunc  = (buttonIndex: Int) -> Void
    
    class CompleteAlertViewFuncClass: NSObject {
        var completeAlertViewFunc: CompleteAlertViewFunc?
        
        init(completeAlertViewFunc: CompleteAlertViewFunc?){
            self.completeAlertViewFunc = completeAlertViewFunc
        }
        
        func copyWithZone(zone: NSZone) -> AnyObject {
            return CompleteAlertViewFuncClass(completeAlertViewFunc: self.completeAlertViewFunc)
        }
    }
    
    extension UIAlertView: UIAlertViewDelegate{
        
        private static var key = "AlertViewComplete"
        
        func showAlertViewWithCompleteBlock(alertViewComplete: CompleteAlertViewFunc! ){
            if alertViewComplete != nil{
                objc_removeAssociatedObjects(self)
                objc_setAssociatedObject(self, &UIAlertView.key, CompleteAlertViewFuncClass(completeAlertViewFunc: alertViewComplete) as AnyObject, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
                self.delegate = self
            }
            self.show()
        }
        
        public func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
            let completeAlertViewFuncObj: CompleteAlertViewFuncClass? = objc_getAssociatedObject(self, &UIAlertView.key) as? CompleteAlertViewFuncClass
            
            if completeAlertViewFuncObj != nil && completeAlertViewFuncObj?.completeAlertViewFunc != nil{
                completeAlertViewFuncObj!.completeAlertViewFunc!(buttonIndex: buttonIndex)
            }
        }
    }
    

      

    UIActionSheet:

    //
    //  UIActionSheet+Block.swift
    //  ECExpert
    //
    //  Created by Fran on 15/6/13.
    //  Copyright (c) 2015年 Fran. All rights reserved.
    //
    
    import Foundation
    
    typealias CompleteActionSheetFunc = (buttonIndex: Int) -> Void
    
    class CompleteActionSheetFuncClass: NSObject {
        var completeActionSheetFunc: CompleteActionSheetFunc?
        
        init(completeActionSheetFunc: CompleteActionSheetFunc?){
            self.completeActionSheetFunc = completeActionSheetFunc
        }
        
        func copyWithZone(zone: NSZone) -> AnyObject {
            return CompleteActionSheetFuncClass(completeActionSheetFunc: self.completeActionSheetFunc)
        }
    }
    
    extension UIActionSheet: UIActionSheetDelegate {
        
        private static var key = "ActionSheetComplete"
        
        func showActionSheetWithCompleteBlock(inView: UIView, completeActionSheetFunc: CompleteActionSheetFunc!){
            if completeActionSheetFunc != nil{
                objc_removeAssociatedObjects(self)
                objc_setAssociatedObject(self, &UIActionSheet.key, CompleteActionSheetFuncClass(completeActionSheetFunc: completeActionSheetFunc) as AnyObject, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
                self.delegate = self
            }
            self.showInView(inView)
        }
        
        public func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
            let completeActionSheetFuncObj: CompleteActionSheetFuncClass? = objc_getAssociatedObject(self, &UIActionSheet.key) as? CompleteActionSheetFuncClass
            
            if completeActionSheetFuncObj != nil && completeActionSheetFuncObj?.completeActionSheetFunc != nil{
                completeActionSheetFuncObj!.completeActionSheetFunc!(buttonIndex: buttonIndex)
            }
        }
    }
    

      

  • 相关阅读:
    HTML Window.document对象
    HTML JavaScript的DOM操作
    HTML 运算符、类型转换
    HTML JavaScript简介
    js对象学习
    理解MySQL数据库覆盖索引
    mysql 索引2
    mysql 索引
    Extjs 使用图标字体来美化按钮)
    MySql数据类型问题
  • 原文地址:https://www.cnblogs.com/FranZhou/p/5086011.html
Copyright © 2011-2022 走看看