Ios 为什么警报解除在再次触发时不起作用?

Ios 为什么警报解除在再次触发时不起作用?,ios,swift,uialertview,uialertcontroller,Ios,Swift,Uialertview,Uialertcontroller,我在向服务器发出请求之前显示加载程序。收到应答后,调用停止加载程序的功能 第一次它工作正常。但是,当您重新进入页面时,加载程序不会关闭,它会一直旋转。(我认为这是因为请求已经发生得更快了,而且没有明显的延迟) 为了清晰起见,我在代码中添加了标记。 请告诉我问题是什么,我如何解决 var alert : UIAlertController? func showLoader(){ self.alert = UIAlertController(title: nil, message: NSL

我在向服务器发出请求之前显示加载程序。收到应答后,调用停止加载程序的功能

第一次它工作正常。但是,当您重新进入页面时,加载程序不会关闭,它会一直旋转。(我认为这是因为请求已经发生得更快了,而且没有明显的延迟)

为了清晰起见,我在代码中添加了标记。
请告诉我问题是什么,我如何解决

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**//首次尝试
**显示警报**
**文件存在**
**请求完成**
**加载程序isBeingPresented=false**
**加载程序正在被解除1=false**
**加载器正在被解除2=true**
**装载机已关闭**
**已选择:2019-03-01 17:00:00+0000**//第二次尝试
**显示警报**
**文件存在**
**请求完成**
**加载程序isBeingPresented=false**
**加载程序正在被解除1=false**
**加载程序正在被解除2=false**
**装卸工被解雇**


以下是您应该如何实现完成块:

1) viewDidLoad调用controller.start() 2) 演示者通过完成回调调用view.showLoader() 3) 在回调中启动
NetworkController.getfiles()

4) 完成NetworkController.getfiles()后,
controller.callback
调用
view.stopLoader()

以下是应如何实现完成块:

1) viewDidLoad调用controller.start() 2) 演示者通过完成回调调用view.showLoader() 3) 在回调中启动
NetworkController.getfiles()


4) 完成NetworkController.getfiles()后,
controller.callback
调用
view.stopLoader()

重新进入页面意味着viewdidload是否被调用
loadingIndicator
是一个局部变量-在哪里/如何停止它?在
showLoader
function@Anbu.Karthik当然可以。我转到另一个视图控制器,然后返回。viewDidLoad调用再次被调用。@亚历克斯·基洛夫:你确定你正在主线程上调用
stopLoader
showLoader
??再添加一条调试语句,打印
Thread.currentThread
,并检查它是否总是在main上调用thread@Paulw11是的,但这只是一个指标。在警报中,我试图解除重新进入页面意味着viewdidload是否被调用
loadingIndicator
是一个局部变量-在哪里/如何停止它?在
showLoader
function@Anbu.Karthik当然可以。我转到另一个视图控制器,然后返回。viewDidLoad调用再次被调用。@亚历克斯·基洛夫:你确定你正在主线程上调用
stopLoader
showLoader
??再添加一条调试语句,打印
Thread.currentThread
,并检查它是否总是在main上调用thread@Paulw11是的,但这只是一个指标。是的,我试过了。结果是一样的。1) 在viewDidLoad方法中,我调用controller.start()2),然后在controller.start()中,我在presenter.waitResponse()3)presenter之后调用NetworkController.getfiles(),presenter只调用view.showLoader()4)当NetworkController.getfiles()完成时,它调用controller.callback5)中的controller.callback,我调用presenter.requestDone()6)演示者只需调用stopLoader(),在第二次调用stopLoader方法后,尝试打印以下self.presentingViewController self.presentedViewController self.alert?。在调用取消行后显示。还可以使用完成处理程序实现disclose调用,并在完成块中执行self.alert=nil。self.presentingViewController两次都为nil。self.alert?.isBeingPresented两次都为false。但是self.presentedViewController第一次存在,第二次为零。此外,我还尝试在DispatchQueue.main.asyncAfter(截止日期:.now()+2.0)中调用self.disclose,效果很好。但我认为这不是一个好的解决方案。好的,这是有道理的。self.presentedViewController为零秒意味着警报尚未出现,这就是解雇通知无效的原因。您应该在“出席”和“驳回”通话中执行完成操作,这将解决问题。另外,在网络请求开始之前应该调用view.showLoader,是这样吗?是的,我试过了。结果是一样的。1) 在viewDidLoad方法中,我调用controller.start()2),然后在controller.start()中,我在presenter.waitResponse()3)presenter之后调用NetworkController.getfiles(),presenter只调用view.showLoader()4)当NetworkController.getfiles()完成时,它调用controller.callback5)中的controller.callback,我调用presenter.requestDone()6)演示者只需调用stopLoader(),在第二次调用stopLoader方法后,尝试打印以下self.presentingViewController self.presentedViewController self.alert?。在调用取消行后显示。还可以使用完成处理程序实现disclose调用,并在完成块中执行self.alert=nil。self.presentingViewController两次都为nil。self.alert?.isBeingPresented两次都为false。但是self.presentedViewController第一次存在,第二次为零。此外,我还尝试在DispatchQueue.main.asyncAfter(截止日期:.now()+2.0)中调用self.disclose,效果很好。但我认为这不是一个好的解决方案。好的,这是有道理的。self.presentedViewController为零秒意味着警报尚未出现,这就是解雇通知无效的原因。您应该在当前通话和取消通话中实现完成,这应该