swift - UIAlertController : it does not close in a controlleView that a UICollectionView

标签 swift uicollectionview uicollectionviewcell swift4 uialertcontroller

我创建了一个类“Chargement”,它允许显示带有加载的警报并关闭它。

import Foundation
class Chargement {
    var alert : UIAlertController;
    var message : String = NSLocalizedString("PleaseWait", comment:"") ;
    let loadingIndicator : UIActivityIndicatorView;
    init(message : String?) {
        if(message != nil){
            self.message = message!;
        }
        self.alert = UIAlertController(title: nil, message: self.message, preferredStyle: UIAlertControllerStyle.alert)
        self.loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 4, width: 40, height: 60))

    }
    func showLoading(view : UIViewController){
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating();
        alert.view.addSubview(loadingIndicator)
        view.present(alert, animated: true, completion: nil)
    }
    func closeLoading(){
        alert.dismiss(animated: true, completion: nil)
    }
}

当我在没有 UICollectionView 的 Controller View 中使用我的类“Chargement”时,它的工作原理如下:

class ViewController : UIViewController{
 let loadingView : Chargement = Chargement(message: nil);
 override func viewDidLoad() {
   super.viewDidLoad();
   loadingView.showLoading(view: self);
  }

  @IBAction func btn_Action(_ sender: UIButton) {
        self.loadingView.closeLoading();//it works
    }
}

但是当我在带有 UICollectionView 的 Controller View 中使用它时,警报显示可以工作,但不会关闭。

class TTwoViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout  {
 let loadingView : Chargement = Chargement(message: nil);
 @IBOutlet weak var collectionView: UICollectionView!
 override func viewDidLoad() {
        super.viewDidLoad();
        loadingView.showLoading(view: self);
        loadingData();
    }
 func loadingData() {
        dataManager().getData(num: 1, completion: {
            (data, messages) in
                   self.loadingView.closeLoading(); //it don't work
                DispatchQueue.main.async(execute: {
                    self.collectionView.reloadData();
                   //self.loadingView.closeLoading(); //it don't work
                })
        })
    }

}

最佳答案

如果您只想显示事件指示器,您可以使用 NVActivityIndicatorView 来简化整个过程。 :

override func viewDidLoad() {
    super.viewDidLoad();
    let activityData = ActivityData()
    NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
    NVActivityIndicatorPresenter.sharedInstance.setMessage("Please Wait")
    loadingData();
}

func loadingData() {
    dataManager().getData(num: 1, completion: { (data, messages) in
        DispatchQueue.main.async {
             self.collectionView.reloadData()
             NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
        }
    })
}

关于swift - UIAlertController : it does not close in a controlleView that a UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49096876/

相关文章:

ios - Collection View 中标签的中心词

ios - UICollectionView 重新加载不正确

ios - swift 中的 UICellView 单元格布局

swift - 在 Swift 中实现 "Either"结构

ios - 为什么 tableview 没有显示正确的图像?

ios - UICollectionView 只返回一个部分?

ios - 在哪里确定动态大小的 UICollectionViewCell 的高度?

ios - Firebase Analytics 多次触发

ios - 当 RevealView Controller 不存在时,如何使用 swrevealviewController 显示侧面菜单?

ios - 在 iPhone 6 上更改为 24 小时制,iOS 9 导致应用程序崩溃