【发布时间】:2017-04-19 12:07:47
【问题描述】:
我有一个 unwind segue,它在将图像保存到磁盘时需要几秒钟才能完成。我想显示一个活动指示器,直到视图被关闭但视图没有更新。
这是我的函数,它在视图控制器被解除之前被调用:
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 中运行相同的代码可以正确地将它显示在屏幕中间。
更新
我已经使用委托重新创建了 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)
}
【问题讨论】:
-
您是否在代码中的某个位置调用了
performSegue? -
谢谢艾哈迈德。是的,我尝试了各种触发 segue 的方法,但仍然遇到同样的问题。