在swift for iOS 7.1+中使用UIAlertView(Using UIAlertView in swift for iOS 7.1+)
我有一些代码适用于iOS 8但不适用于iOS 7.1我不确定iOS 7.1的代码需要什么
这是我目前拥有的在iOS 7.1上无效的内容
let alert = UIAlertController(title: "no connection", message: "dude, connect to the internet!", preferredStyle: UIAlertControllerStyle.Alert) alert!.addAction(UIAlertAction(title: "Ok!", style: .Cancel, handler: { (ac: UIAlertAction!) -> Void in alert?.dismissViewControllerAnimated(true, completion: nil) })) self.presentViewController(alert!, animated: true, completion: nil)这就是我试图让UIAlertView在iOS 7.1上工作的原因
let alert = UIAlertView() alert.title = "no connection" alert.message = "dude, connect to the internet!" alert.show()I have some code that works on iOS 8 but not on iOS 7.1 I am unsure what the code needs to look like for iOS 7.1
here's what I currently have that doesn't work on iOS 7.1
let alert = UIAlertController(title: "no connection", message: "dude, connect to the internet!", preferredStyle: UIAlertControllerStyle.Alert) alert!.addAction(UIAlertAction(title: "Ok!", style: .Cancel, handler: { (ac: UIAlertAction!) -> Void in alert?.dismissViewControllerAnimated(true, completion: nil) })) self.presentViewController(alert!, animated: true, completion: nil)here's what I have tried to get UIAlertView to work on iOS 7.1+
let alert = UIAlertView() alert.title = "no connection" alert.message = "dude, connect to the internet!" alert.show()最满意答案
看起来你想要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") } }Looks like you want UIAlertView to have a nice completion handler. I do not care for the delegate pattern when using them, so I have come up with my own extension:
UIAlertView Extension (Completion Handler)
// 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)
// 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) } }Example Usage
// 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") } }更多推荐
发布评论