在 swift 中为 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()
UIAlertController 仅在 ios8 或更高版本中可用,根据 Apple docs。您将需要在 ios7.
中使用 UIAlertView
更新
抱歉,我没有完全理解你的问题。
要构建 UI 只需执行以下操作
let alert = UIAlertView(title: "no connection", message: "dude, connect to the internet!", delegate: self, cancelButtonTitle: "Ok!")
alert.show()
然后确保在 VC 中实施 UIAlertViewDelegate。以上应该会自动解除警报,但如果没有,您可以实施
alertView(_ alertView: UIAlertView,clickedButtonAtIndex buttonIndex: Int)
并从那里解除警报
看起来您希望 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(私有 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)
}
}
用法示例
// 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")
}
}
我有一些代码适用于 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()
UIAlertController 仅在 ios8 或更高版本中可用,根据 Apple docs。您将需要在 ios7.
中使用 UIAlertView更新
抱歉,我没有完全理解你的问题。
要构建 UI 只需执行以下操作
let alert = UIAlertView(title: "no connection", message: "dude, connect to the internet!", delegate: self, cancelButtonTitle: "Ok!")
alert.show()
然后确保在 VC 中实施 UIAlertViewDelegate。以上应该会自动解除警报,但如果没有,您可以实施
alertView(_ alertView: UIAlertView,clickedButtonAtIndex buttonIndex: Int)
并从那里解除警报
看起来您希望 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(私有 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)
}
}
用法示例
// 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")
}
}