【问题标题】:How to call my custom alert controller function to display in other view controller?如何调用我的自定义警报控制器功能以显示在其他视图控制器中?
【发布时间】:2015-06-15 04:12:07
【问题描述】:

我已经编写了我的自定义警报视图控制器。但是,我在从其他视图控制器调用我的警报控制器时出错。它显示了我在下面描述的错误。

控制台错误

2015-06-15 10:21:50.610 automobile[4197:62165] Warning: Attempt to present <UIAlertController: 0x7d11cf70> on <automobile.LoadingAlertViewController: 0x7bfa55d0> whose view is not in the window hierarchy!

这是我的LoadingAlertViewController

import UIKit

class LoadingAlertViewController: UIAlertController {

var loadingAlertController : UIAlertController!

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

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

func displayLoadingAlert() -> UIAlertController {

    var controllerToPresent = viewController
    if controllerToPresent == nil {
            controllerToPresent = self
    }
    //create an alert controller
    loadingAlertController = UIAlertController(title: "Loading...", message: "We are receiving data from network.Please Wait.", preferredStyle: .Alert)

    let indicator = UIActivityIndicatorView()
    indicator.color = UIColor.redColor()
    indicator.setTranslatesAutoresizingMaskIntoConstraints(false)
    loadingAlertController.view.addSubview(indicator)

    let views = ["pending" : loadingAlertController.view, "indicator" : indicator]
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[indicator]-(7)-|", options: nil, metrics: nil, views: views)
    constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[indicator]|", options: nil, metrics: nil, views: views)
    loadingAlertController.view.addConstraints(constraints)

    indicator.userInteractionEnabled = false
    indicator.startAnimating()

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

    return loadingAlertController
}

func dismissLoadingAlert() -> UIAlertController {
    loadingAlertController.dismissViewControllerAnimated(true, completion: nil)
    return loadingAlertController
}

}

然后我在我的另一个视图控制器上 decalare 这个LoadingAlertViewController,每次我向我的 API 请求时我想在该控制器中显示。

这是我的ViewController

import UIKit

class ViewController : UIViewControler{

var loadingAlertController : LoadingAlertController!
var api = MyAPI()

override func viewDidLoad() {

       loadingAlertController = LoadingAlertViewController()

       api.getResults()

       showAlert(true)

}

func showAlert(alert : Bool){

       if alert{

          loadingAlertController.displayLoadingAlert(self)

       }else{

          loadingAlertController.dismissLoadingAlert()

       }

}

任何帮助?请?如果有办法,如何从另一个视图控制器调用它。谢谢

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    像这样改变你的方法:

    func displayLoadingAlert(viewController: UIViewController?) -> UIAlertController {
        var controllerToPresent = viewController
        if controllerToPresent == nil {
        controllerToPresent = self
    }
    
    // Most of your code
    
    controllerToPresent.presentViewController(loadingAlertController, animated: true, completion: nil)
    
    return loadingAlertController
    }
    

    然后当你调用警报时:

    loadingAlertController.displayLoadingAlert(self)
    

    或者: 将方法 displayLoadingAlert 重命名为 loadingAlert

    删除线:

    self.presentViewController(loadingAlertController, animated: true, completion: nil)
    

    然后在调用showAlert()方法时

    let loadingAlertController = loadingAlertController.loadingAlert()
    self.presentViewController(loadingAlertController, animated: true, completion: nil)
    

    【讨论】:

    • 它工作。我也更新了答案。但是,我不太明白你说的“然后在调用 showAlert() 方法时......”的话。
    • 第二个代码块中的方法 showAlert(alert: Bool) 方法。
    • 哦,我明白了。但是,我想我现在不需要这样做。我编辑了您的答案,现在对我来说非常有用。感谢您的帮助,@David Wong。
    • ,你也可以帮我解决这个问题。stackoverflow.com/questions/30840235/…
    【解决方案2】:

    我看到你是作为子类制作的,但是在UIAlertController 文档中是这样写的:

    UIAlertController 类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的,并且 不得修改。

    我建议你不要反对它。如果您需要一些非常自定义的东西来更好地使用 UIViewController 并以自定义行为显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多