我在向服务器发出请求之前显示加载器。收到答案后 - 调用停止加载程序的功能。
它第一次工作正常。但是当你重新进入页面时,加载器没有关闭,它仍然永远旋转。 (我相信这是因为请求已经发生得更快,而且没有明显的延迟)。
为清楚起见,我在代码中添加了标记。 请告诉我问题是什么,我该如何解决?
var alert : UIAlertController?
func showLoader(){
self.alert = UIAlertController(title: nil, message: NSLocalizedString("pleaseWait", comment: "Текст ожидания загрузки"), preferredStyle: .alert)
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
self.alert!.view.addSubview(loadingIndicator)
present(self.alert!, animated: true, completion: nil)
Logger.Log("Alert is presented")
}
func stopLoader(){
if let _ = self.alert {
Logger.Log("Loader isBeingPresented = \(self.alert!.isBeingPresented)")
Logger.Log("Loader isBeingDismissed1 = \(self.alert!.isBeingDismissed)")
self.alert!.dismiss(animated: false, completion: nil)
Logger.Log("Loader isBeingDismissed2 = \(self.alert!.isBeingDismissed)")
self.alert = nil
Logger.Log("Loader dismissed")
} else {
Logger.Log("Alert failed")
}
}
**选择:2019-03-01 17:00:00 +0000 ** //首先尝试 **提供警报** **文件存在! ** ** requestDone ** ** Loader isBeingPresented = false ** ** Loader isBeingDismissed1 = false ** ** Loader isBeingDismissed2 = true ** **装载机被解雇** **选择:2019-03-01 17:00:00 +0000 ** //第二次尝试 **提供警报** **文件存在! ** ** requestDone ** ** Loader isBeingPresented = false ** ** Loader isBeingDismissed1 = false ** ** Loader isBeingDismissed2 = false ** **装载机被解雇**
以下是如何实现完成块的方法:
1)viewDidLoad调用controller.start()
2)presenter调用带有完成回调的view.showLoader()
3)在回调中启动NetworkController.getfiles()
4)当NetworkController.getfiles()完成时,controller.callback
调用view.stopLoader()