【问题标题】:Dismiss alert view controller in a popover page在弹出页面中关闭警报视图控制器
【发布时间】:2021-11-26 04:53:16
【问题描述】:

在弹出页面中,我试图将一些数据存储在 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)

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

【问题讨论】:

  • 从 presentLoadingView 中取出“让警报”。并调用 alert.dismiss(animated: true, completion: nil)

标签: swift uialertviewcontroller


【解决方案1】:

尝试替换

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

    if shouldPresent {
    presentAnimated(alert)
}

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

【讨论】:

    【解决方案2】:

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

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

    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() 来关闭加载器。

    【讨论】:

      【解决方案3】:

      你可以使用这个技巧

         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)
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2014-08-27
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 1970-01-01
        相关资源
        最近更新 更多