我有一个放松的选择,在将图像保存到磁盘时需要几秒钟才能完成。我想显示一个活动指示器,直到视图被丢弃,但视图未更新。
这是我的功能,它在忽略视图控制器之前从视图控制器中调用:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "saveRecord" {
print("indicator")
let indicator = UIActivityIndicatorView()
indicator.frame = self.view.frame
indicator.activityIndicatorViewStyle = .whiteLarge
indicator.color = UIColor.blue
indicator.hidesWhenStopped = true
indicator.startAnimating()
self.view.addSubview(indicator)
self.view.layoutSubviews()
print("laid out subviews")
}
}
两个打印语句执行,调试器显示指示器已作为子视图添加到主视图中,但没有出现在屏幕上。我想念什么吗?
我知道指示器的位置不是问题,因为在viewDidload中运行相同的代码在屏幕中间正确显示了它。
update
我已经使用委托重新创建了SEGUE功能,它可以正确保存所有内容,但问题仍然存在。仍然没有活动指标。
@IBAction func saveRecord(_ sender: Any) {
print("indicator")
let indicator = UIActivityIndicatorView()
indicator.frame = self.view.frame
indicator.activityIndicatorViewStyle = .whiteLarge
indicator.color = UIColor.blue
indicator.hidesWhenStopped = true
indicator.startAnimating()
self.view.addSubview(indicator)
self.view.layoutSubviews()
print("laid out subviews")
saveImages()
print("saved images")
self.delegate?.saveRecord(name: name!, category: category)
print("saved record")
self.navigationController?.popViewController(animated: true)
}
更新2
我现在真的很困惑!这开始指示:
@IBAction func saveRecord(_ sender: Any) {
print("indicator")
indicator.startAnimating()
//saveImages()
//print("images saved")
//performSegue(withIdentifier: "saveRecord", sender: self)
}
但这不是:
@IBAction func saveRecord(_ sender: Any) {
print("indicator")
indicator.startAnimating()
saveImages()
print("images saved")
performSegue(withIdentifier: "saveRecord", sender: self)
}
问题是,在saverecord函数完成之前,UI不会更新,但是随后称为segue,以便立即解散视图控制器。我使用调度队列解决了它 - 我今天学到的另一个新技能!:
@IBAction func saveRecord(_ sender: Any) {
indicator.startAnimating()
DispatchQueue.global(qos: .userInitiated).async {
self.saveImages()
DispatchQueue.main.async {
self.performSegue(withIdentifier: "saveRecord", sender: self)
}
}
}
我认为您应该使用PopviewController而不是Undind Segue。然后,您可以在后面按钮上使用自定义功能具有更多的控制。
您可以做的是:
- 单击后按钮(或保存映像按钮(时,添加指示器
- 然后保存图像
- 保存图像时删除指示器
- 弹出视图控制器
您无法在prepare(for:)
中更新视图;segue已经在进行中。
,而不是直接从UI元素触发Undind Segue,而是触发一种动作方法,该方法显示活动指示器并执行保存,然后调用performSegue(withIdentifier:"yourUnwindSegueIdentifier", sender:self)
实际执行Undind。