Skip to content

Instantly share code, notes, and snippets.

@mgp
Created February 26, 2015 01:07
Show Gist options
  • Save mgp/650573cfae116e1b4fca to your computer and use it in GitHub Desktop.
Save mgp/650573cfae116e1b4fca to your computer and use it in GitHub Desktop.
/**
A UIAlertView that owns its delegate so that the client that displays the UIAlertView
does not need to own it.
This is useful when the client that displays the UIAlertView is not the UIAlertDelegate
implementation. It is free to initialize an object to serve as the delegate that exists
only for the lifetime of the UIAlertView, thereby fostering decomposition.
*/
class OwnedDelegateAlertView: UIAlertView {
private let ownedDelegate: AnyObject?
override init(title: String?, message: String?, delegate: AnyObject?, cancelButtonTitle: String?) {
ownedDelegate = delegate
super.init(title: title, message: message, delegate: delegate, cancelButtonTitle: cancelButtonTitle)
}
convenience init(title: String, message: String, delegate: UIAlertViewDelegate, cancelButtonTitle: String, otherButtonTitles firstButtonTitle: String, _ moreButtonTitles: String...) {
self.init(title: title, message: message, delegate: delegate, cancelButtonTitle: cancelButtonTitle)
addButtonWithTitle(firstButtonTitle)
moreButtonTitles.each { self.addButtonWithTitle($0); return }
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) { super.init(frame: frame) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment