如何调用我的自定义警报控制器功能以显示在其他视图控制器中?
How to call my custom alert controller function to display in other view controller?
我已经编写了我的自定义警报视图 controller.But,我在从其他视图调用我的警报控制器时遇到错误 controller.It 显示我在下面描述的错误。
控制台错误
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()
}
}
有什么帮助吗?拜托?如果有办法,如何从另一个视图调用它controller.Thank你
像这样改变你的方法:
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)
我看到你做了子类,但是在UIAlertController文档中写着:
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.
我建议你不要反对它。如果您需要一些非常自定义的东西,最好使用 UIViewController 并以自定义行为显示。
我已经编写了我的自定义警报视图 controller.But,我在从其他视图调用我的警报控制器时遇到错误 controller.It 显示我在下面描述的错误。
控制台错误
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()
}
}
有什么帮助吗?拜托?如果有办法,如何从另一个视图调用它controller.Thank你
像这样改变你的方法:
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)
我看到你做了子类,但是在UIAlertController文档中写着:
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.
我建议你不要反对它。如果您需要一些非常自定义的东西,最好使用 UIViewController 并以自定义行为显示。