gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:43:20 26 4
gpt4 key购买 nike

我创建了一个类“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com