如何调用我的自定义警报控制器功能以显示在其他视图控制器中? [英] How to call my custom alert controller function to display in other view controller?
问题描述
我已经编写了自定义警报视图控制器。但是,我在从其他视图控制器调用警报控制器时出错。它显示了我在下面描述的错误。
I have write my custom alert view controller.But,I am having error at calling my alert controller from other view controller.It show me the error that I described below.
控制台错误
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
}
}
然后我在我的另一个视图控制器上解除了这个 LoadingAlertViewController
,我想在每次向我请求时显示API。
Then I decalare this LoadingAlertViewController
at my another view controller where i want to display every time that i request to my 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()
}
}
任何帮助?请?如果有办法,如何从另一个方式拨打电话查看控制器。谢谢
Any Help?Please?If there is a way,how to call it from another view controller.Thank you
推荐答案
更改方法如下:
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
Alternatively: Rename the method displayLoadingAlert to loadingAlert
删除行:
self.presentViewController(loadingAlertController, animated: true, completion: nil)
然后在showAlert()方法中调用
then when calling insidethe showAlert() method
let loadingAlertController = loadingAlertController.loadingAlert()
self.presentViewController(loadingAlertController, animated: true, completion: nil)
这篇关于如何调用我的自定义警报控制器功能以显示在其他视图控制器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!