【发布时间】: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