调用Prepare for Segue时,SWIFT 3-view不更新 [英] Swift 3 - view doesn't update when prepare for segue is called

查看:19
本文介绍了调用Prepare for Segue时,SWIFT 3-view不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个展开段,它在将图像保存到磁盘时需要几秒钟才能完成。我想显示活动指示器,直到关闭该视图,但该视图不更新。

这是我的函数,在解除它之前从视图控制器调用:

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")
    }
}

执行两条print语句,调试器显示指示器已作为子视图添加到主视图中,但它不会出现在屏幕上。我错过了什么吗?

我知道指示器的位置不是问题,因为在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)
}

推荐答案

问题是,在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)
        }
    }
}

这篇关于调用Prepare for Segue时,SWIFT 3-view不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆