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)
            }
        }
    }
    

      

  • 相关阅读:
    WPF应用程序使用资源及多语言设置学习1
    在Nhibernate.Burrow中使用Fluent NHibernate
    预加载页面图片的简单方案
    IIS7中使用Nhibernate Burrow的问题.
    WPF应用程序使用资源及多语言设置学习2
    xampp中apache在windows7下无法启动的问题
    cache和buffer
    Python基础题
    Python 数据类型
    简单的Nginx自动化安装啊脚本
  • 原文地址:https://www.cnblogs.com/FranZhou/p/5086011.html
Copyright © 2011-2022 走看看