看起来你希望UIAlertView 有一个很好的完成处理程序。使用它们时我不关心委托模式,所以我想出了自己的扩展:
UIAlertView 扩展(完成处理程序)
// Used by objc_getAssociatedObject
private var UIAlertViewWrapperPropertyKey : UInt8 = 0
typealias AlertViewCompletionHandler = (alertView: UIAlertView, buttonIndex: Int) -> Void
extension UIAlertView
{
// MARK: - Associated Properties
private var wrapper : UIAlertViewWrapper?
{
get { return objc_getAssociatedObject(self, &UIAlertViewWrapperPropertyKey) as? UIAlertViewWrapper }
set { objc_setAssociatedObject(self, &UIAlertViewWrapperPropertyKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) }
}
// MARK - Convenience Initializers
convenience init(title: String?, message: String?, cancelButtonTitle: String?)
{
self.init(title: title, message: message, delegate: nil, cancelButtonTitle: cancelButtonTitle)
}
convenience init(title: String?, message: String?, cancelButtonTitle: String?, otherButtonTitles: String...)
{
self.init(title: title, message: message, delegate: nil, cancelButtonTitle: cancelButtonTitle)
for buttonTitle in otherButtonTitles { self.addButtonWithTitle(buttonTitle) }
}
// MARK: - Show with Completion Handler
func showWithCompletion(_ completionHandler: AlertViewCompletionHandler? = nil)
{
self.wrapper = UIAlertViewWrapper(completionHandler: completionHandler)
self.delegate = self.wrapper
self.show()
}
// MARK: - Show Class Methods
class func showWithTitle(title: String?, message: String?, cancelButtonTitle: String?, completionHandler: AlertViewCompletionHandler? = nil)
{
showWithTitle(title, message: message, cancelButtonTitle: cancelButtonTitle, otherButtonTitles: nil, completionHandler: completionHandler)
}
class func showWithTitle(title: String?, message: String?, cancelButtonTitle: String?, otherButtonTitle: String?, completionHandler: AlertViewCompletionHandler? = nil)
{
let otherButtonTitles : [String]? = otherButtonTitle != nil ? [otherButtonTitle!] : nil
showWithTitle(title, message: message, cancelButtonTitle: cancelButtonTitle, otherButtonTitles: otherButtonTitles, completionHandler: completionHandler)
}
class func showWithTitle(title: String?, message: String?, cancelButtonTitle: String?, otherButtonTitles: [String]?, completionHandler: AlertViewCompletionHandler? = nil)
{
let alertView = UIAlertView(title: title, message: message, delegate: nil, cancelButtonTitle: cancelButtonTitle)
if let otherButtonTitles = otherButtonTitles
{
for buttonTitle in otherButtonTitles
{
alertView.addButtonWithTitle(buttonTitle)
}
}
alertView.showWithCompletion(completionHandler)
}
}
UIAlertViewWrapper(私有类)
// Private class that handles delegation and completion handler (do not instantiate)
private final class UIAlertViewWrapper : NSObject, UIAlertViewDelegate
{
// MARK: - Completion Handlers
var completionHandler: AlertViewCompletionHandler?
// MARK: - Initializers
init(completionHandler: AlertViewCompletionHandler?)
{
self.completionHandler = completionHandler
}
// MARK: - UIAlertView Delegate
private func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
{
completionHandler?(alertView: alertView, buttonIndex: buttonIndex)
}
}
示例用法
// You can use class function to call the UIAlertView
UIAlertView.showWithTitle("Hello", message: "Hello World", cancelButtonTitle: "Okay") { alertView, buttonIndex in
// Do something when the alert view is clicked
}
// Or... you can instantiate one and use the showWithCompletion method
let yesNoMaybeAlertView = UIAlertView(title: "Choice", message: "Pick one", cancelButtonTitle: "No", otherButtonTitles: "Yes", "Maybe")
yesNoMaybeAlertView.showWithCompletion { alertView, buttonIndex in
switch buttonIndex
{
case 1: println("Yes")
case 2: println("Maybe")
default: println("No")
}
}