我试图在用户点击某个 TabBar 项目时显示事件指示器。我认为我的问题在于 UI 主线程被卡住。
当用户点击 TabBar 时,我会准备一个大数据列表,大约需要六秒钟。我让事件显示在任何地方,但当他们点击 TabBar 时。
指示器似乎正在“运行”,因为当 segued uitableviewcontroller 显示时,它正在显示指示器。但这已经太晚了,调度似乎也没有做任何事情。
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let visibleViewCtrl = UIApplication.shared.keyWindow?.visibleViewController {
// do whatever you want with your `visibleViewCtrl`
print (String.init(describing: visibleViewCtrl.classForCoder))
DispatchQueue.main.async{
let aprogressView = ProgressView(Message: "Filtering...",
Theme:.Dark,
IsModal:true);
visibleViewCtrl.view.addSubview(aprogressView)
aprogressView.show()
}
}
return true
}
好吧,所以问题是它会转到一个 TableView Controller ,它几乎立即尝试获取节中的行,然后首先执行 fetchrequest,该请求会阻塞主线程上的所有内容。
最佳答案
已解决:
添加了一个 bool 值:
var loadData = false
override func viewDidAppear(_ animated: Bool) {
loadData = true
self.tableView.reloadData()
}
然后代表们:
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if loadData == false {
return nil
}
return searchFetchedResultsController.sectionIndexTitles
}
func numberOfSections(in tableView: UITableView) -> Int {
if loadData == false {
return 0
}
现在它将立即转换到新 Controller ,因此我现在可以显示来自 viewWillAppear 的反馈:
appDelegate.progressView = ProgressView(Message: "Filtering Brands", Theme:.Dark, IsModal:true);
appDelegate.progressView!.show()
这不适用于程序化显示
self.navigationController?.pushViewController(vc, animated: false)
关于swift - 点击 TabBar 时更新当前 UIViewController UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54524933/