0

在弹出页面中,我试图在 CoreData 中存储一些数据,并且我需要显示加载视图,直到该过程完成。
我找到了一种简单易用的方法,使用警报控制器显示加载标签。在函数中,我添加了一个shouldPresent布尔值以在 coreData 过程完成时使其为 false 并隐藏警报。

private func presentLoadingView(shouldPresent: Bool) {
     let alert = UIAlertController(title: nil, message: "Retrieving Owner Data", preferredStyle: .alert)
     let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 3, y: 5, width: 50, height: 50))
     loadingIndicator.hidesWhenStopped = true
     loadingIndicator.style = UIActivityIndicatorView.Style.medium
     loadingIndicator.startAnimating()

     alert.view.addSubview(loadingIndicator)
     presentAnimated(alert)
        
     if !shouldPresent {
        alert.dismiss(animated: true)
     }
  }

问题是,当我使用

dismiss(animated: true)

整个弹出框将被解除,当我使用

alert.dismiss(animated: true)

什么都没有发生,谁能帮我解决这个问题。提前致谢。

4

3 回答 3

0

我认为使用警报视图进行加载不足以满足您的需求。

您可以轻松地使用此扩展程序来显示加载视图。

extension UIViewController{
    func showActivityIndicator(){
        DispatchQueue.main.async {[weak self] in
            guard let self = self else {return}
            let indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            indicator.backgroundColor = UIColor.init(hexString: "#010101").withAlphaComponent(0.6)
            indicator.layer.cornerRadius = 6
            indicator.center = self.view.center
            indicator.hidesWhenStopped = true
            indicator.color = UIColor(hexString: "#FFEE00")
            indicator.style = UIActivityIndicatorView.Style.large
            indicator.startAnimating()
            self.view.isUserInteractionEnabled = false
            indicator.tag = 1000000
            for subView in self.view.subviews{
                if subView.tag == 1000000{
                    print("Already Added")
                    return
                }
            }
            self.view.addSubview(indicator)
        }
    }
    func hideActivityIndicator(){
        DispatchQueue.main.async { [weak self] in
            guard let self = self else {return}
            let indicator = self.view.viewWithTag(1000000) as? UIActivityIndicatorView
            indicator?.stopAnimating()
            indicator?.removeFromSuperview()
            self.view.isUserInteractionEnabled = true
            indicator?.isHidden = true
        }
    }
}

在您的视图控制器中,当您想要显示加载器时调用 self.showActivityIndi​​cator() 并调用self.hideActivityIndi ​​cator()来关闭加载器。

于 2021-10-06T11:48:13.490 回答
0

您可以使用此技术

   var alert:UIAlertController? = UIAlertController()  //set variable outside in your popover


   private func presentLoadingView(shouldPresent: Bool)  {
        if shouldPresent {
            if self.alert == nil{
                self.alert = UIAlertController(title: nil, message: "Retrieving Owner Data", preferredStyle: .alert)
                let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 3, y: 5, width: 50, height: 50))
                loadingIndicator.hidesWhenStopped = true
                loadingIndicator.style = UIActivityIndicatorView.Style.medium
                loadingIndicator.startAnimating()

                alert.view.addSubview(loadingIndicator)
            }
            if !self.alert.isBeingPresented {
                self.present(self.alert, animated: true, completion: nil)
            }
            
        }else{
            self.alert.dismiss(animated: false, completion: nil)
        }
    }
于 2021-10-06T13:08:18.140 回答
0

尝试更换

    presentAnimated(alert)
   
if !shouldPresent {
   alert.dismiss(animated: true)
}

    if shouldPresent {
    presentAnimated(alert)
}

根据您的示例代码,它似乎可以完成工作。

于 2021-10-06T08:35:33.897 回答