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