繁体   English   中英

在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()

看起来您希望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")
    }
}

根据Apple文档 ,UIAlertController仅在ios8或更高版本中可用。 您将需要在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)

并从那里消除警报

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM