ios - 无法完全关闭我以前的 alertcontroller

标签 ios swift uialertcontroller

我正在尝试使用一个警报 Controller ,其中包括多个警报显示功能和一个关闭功能。但我有 this warning在我的控制台和我的其他警报不显示。我想知道为什么?以及解决方案。

这是我的警报 Controller

import UIKit

class MyAlertViewController: UIViewController {

var myAlertController : UIAlertController!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func displayLoadingAlert(viewController: UIViewController?) -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
        controllerToPresent = self
    }

    //create an alert controller
    myAlertController = UIAlertController(title: "Loading...", message: “We are receiving data,please wait“, preferredStyle: .ActionSheet)

    controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)

    return myAlertController
}

func connectionErrorAlert(viewController: UIViewController?) -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
        controllerToPresent = self
    }

    //create an alert controller
    myAlertController = UIAlertController(title: "Not Connected", message: “No internet Connection”, preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil)
    myAlertController.addAction(defaultAction)

    controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)

    return myAlertController
}

func requestTimeOutErrorAlert(viewController: UIViewController?) -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
        controllerToPresent = self
    }

    //create an alert controller
    myAlertController = UIAlertController(title: "Request Time Out", message: “Please Click Retry”, preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil)
    myAlertController.addAction(defaultAction)

    controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)

    return myAlertController
}

func dismissLoadingAlert(){
    myAlertController.dismissViewControllerAnimated(true, completion: nil)
}

}

当我从 API 获得结果时,我曾经执行 dismissLoadingAlert()。但是,当我没有从 API 获得结果时。我过去常常从我的协议(protocol)中执行这个委托(delegate)方法。

func didNotReceiveAPIResults(results: Bool,error:NSError){
    dispatch_async(dispatch_get_main_queue(), {
        // This condition will be enter if we dont get the results and show user with alert.
        if (results) {
            // I have done dismiss first data receiving alert,what do i doing wrong?
            self.myAlertController.dismissLoadingAlert()
            if error.localizedDescription == "The Internet connection appears to be offline."{
                self.myAlertController.connectionErrorAlert(self)
                self.carTableView.hidden=true
                self.retryButton?.hidden=false
                self.retryButton?.enabled=true
            }
            if error.localizedDescription == "Request Time Out."{
                self.myAlertController.requestTimeOutErrorAlert(self)
                self.carTableView.hidden=true
                self.retryButton?.hidden=false
                self.retryButton?.enabled=true
            }

        }else{
            self.myAlertController.displayLoadingAlert(self)
            self.retryButton?.hidden=true
            self.retryButton?.enabled=false
            self.carTableView.hidden=false
            self.carTableView.reloadData()
        }
    })
}

最佳答案

你走错了路。您不应该子类化该 Controller 。

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

如果您需要一个方便的类,请使用呈现动态 UIAlertController 实例的 NSObject 子类进行重构。

关于ios - 无法完全关闭我以前的 alertcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30840235/

相关文章:

ios - UIResponder didNotRecognizeSelector:应用程序因此错误而崩溃

ios - 如何通过 Storyboard(xib?)和以编程方式 Xamarin.iOS 制作弹出窗口

ios - NSFetch请求 : Return dictionary with nested arrays of dictionaries

将 inout 值转换为 UnsafeMutablePointer<Unmanaged<TYPE>?>

ios - xcode 显示所有源代码文件中的所有错误?

ios - swift enum 在单个 switch-case 中获取具有相同参数的多个 case 的关联值

ios - 检测警报按钮点击事件

ios - 如何在 UIAlertController 中使用 UITextView

ios - 如何在单击按钮时显示和隐藏 View

iphone - 向 UIToolbar 添加两个 UITextField