【发布时间】: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