美文网首页
Swift用block响应UIAlertView和UIActio

Swift用block响应UIAlertView和UIActio

作者: 大雨不晴 | 来源:发表于2016-01-05 16:56 被阅读573次

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

    相关文章

      网友评论

          本文标题:Swift用block响应UIAlertView和UIActio

          本文链接:https://www.haomeiwen.com/subject/uwjzhttx.html