【问题标题】:Using UIAlertView in swift for iOS 7.1+在 iOS 7.1+ 中快速使用 UIAlertView
【发布时间】:2015-03-17 18:59:59
【问题描述】:

我有一些适用于 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()

【问题讨论】:

    标签: ios swift ios7 uialertview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      根据Apple docs,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)
      

      并从那里解除警报

      【讨论】:

      • 我知道,但我不知道如何将我的代码移植到 UIAlertView
      • 您使用 UIAlertView 作为视图。 UIAlertViewDelegate 处理动作 (func alertView(_ alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多