[英]How to update a progress view that is on another UIViewController - SWIFT?
I have a UITableViewController
that is embedded in a UINavigationController
. 我有一个UITableViewController
嵌入在UINavigationController
。 When I click on a row I am attempting to upload a file. 当我单击一行时,我正在尝试上传文件。 To show the progress of this upload I have decided to use custom popup (another UIViewController
) - if anyone has a better idea to show the progress of the upload in this context I am open to it. 为了显示此上传的进度,我决定使用自定义弹出窗口(另一个UIViewController
)-如果有人有更好的主意在这种情况下显示上传的进度,我可以接受。
The only idea I have to transfer continuous data from one UIViewController
to another (if that is possible) is by using a Singleton. 我必须将连续数据从一个UIViewController
传输到另一个(如果可能的话)的唯一想法是使用Singleton。 My code is below, my issue at the moment is I do not know how to update the progress view even though now it has access to the progress data via the singleton. 我的代码在下面,目前我的问题是,即使现在它可以通过单例访问进度数据,我也不知道如何更新进度视图。
class SharedSingleton{
private init(){}
static let shared = SharedSingleton()
var prog: Float = 0
}
UITableViewController: UITableViewController:
Using Alamofire to get the fraction of upload completed: 使用Alamofire来完成部分上传:
/**TRACK PROGRESS OF UPLOAD**/
upload.uploadProgress { progress in
//print(progress.fractionCompleted)
let mySingleton = SharedSingleton.shared
mySingleton.prog = Float(progress.fractionCompleted)
print("mySingleton.prog: \(mySingleton.prog)")
UIViewController containing popup: UIViewController包含弹出窗口:
@IBOutlet weak var popUpContainer: UIView!
@IBOutlet weak var uploadStatus: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// Make popup have rounded corners
popUpContainer.layer.cornerRadius = 5
popUpContainer.layer.masksToBounds = true
// Call Singleton and assign progress of upload to progress view
// HOW TO UPDATE ??????
let mySingleton = SharedSingleton.shared
progressBar.progress = mySingleton.prog
}
One option would be to use NotificationCenter
and in your pop-up view controller subscribe to notifications and in your API callback for the progress, publish a notification. 一种选择是使用NotificationCenter
然后在弹出视图控制器中订阅通知,并在API回调中获取进度,然后发布通知。
For example: 例如:
UploadNotifications.swift
: UploadNotifications.swift
:
import Foundation
extension Notification.Name {
static let UploadProgress = Notification.Name("UploadProgress")
}
UploadProgressViewController.swift
(your pop-up): UploadProgressViewController.swift
(您的弹出窗口):
import UIKit
class UploadProgressViewController: UIViewController {
@IBOutlet weak var progressBar: UIProgressView!
private let progress: Progress = {
let progress = Progress()
progress.completedUnitCount = 0
progress.totalUnitCount = 100
return progress
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.uploadDidProgress(_:)), name: .UploadProgress, object: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
@objc private func uploadDidProgress(_ notification: Notification) {
if let progress = notification.object as? Int64 {
self.progress.completedUnitCount = progress
if progress == 100 {
// dismiss/exit
}
}
}
}
Then in your method with the upload progress callback: 然后在您的方法中使用上载进度回调:
upload.uploadProgress { progress in
NotificationCenter.default.post(name: .SyncDidProgress, object: Int64(progress))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.