Skip to content

Instantly share code, notes, and snippets.

@minsOne
Created August 23, 2015 07:41
Show Gist options
  • Save minsOne/5e63c58fd68a0fb76bae to your computer and use it in GitHub Desktop.
Save minsOne/5e63c58fd68a0fb76bae to your computer and use it in GitHub Desktop.
UIAlertViewClosure 코드
public typealias AVDidDismissClosure = (UIAlertView, Int) -> Void
private var associatedEventHandle: UInt8 = 0
private final class AlertViewClosureWrapper {
private var didDismiss: AVDidDismissClosure?
}
extension UIAlertView: UIAlertViewDelegate {
private var closureWrapper:AlertViewClosureWrapper {
get {
if let wrapper = objc_getAssociatedObject(self, &associatedEventHandle) as? AlertViewClosureWrapper {
return wrapper
}
self.closureWrapper = AlertViewClosureWrapper()
return self.closureWrapper
}
set {
self.delegate = self
objc_setAssociatedObject(self, &associatedEventHandle, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
public var didDismiss: AVDidDismissClosure? {
get { return self.closureWrapper.didDismiss }
set { self.closureWrapper.didDismiss = newValue }
}
public func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
println(__FUNCTION__)
self.closureWrapper.didDismiss?(alertView, buttonIndex)
}
convenience init(title: String, message: String, cancelButtonTitle: String, dismissClosure: AVDidDismissClosure?) {
println(__FUNCTION__)
self.init()
self.title = title
self.message = message
self.addButtonWithTitle(cancelButtonTitle)
self.didDismiss = dismissClosure
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment