【问题标题】:UIAlertController : it does not close in a controlleView that a UICollectionViewUIAlertController :它不会在 UICollectionView 的 controlleView 中关闭
【发布时间】:2018-03-04 15:17:14
【问题描述】:

我创建了一个“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 的控制器视图中使用我的类“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 的controllerView 中使用它时,警报显示可以工作,但不会关闭。

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

}

【问题讨论】:

    标签: swift uicollectionview uicollectionviewcell swift4 uialertcontroller


    【解决方案1】:

    如果你只是想显示一个活动指示器,你可以使用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()
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 2015-05-09
      • 2017-08-04
      相关资源
      最近更新 更多