一种选择是使用
NotificationCenter
在弹出式视图控制器中订阅通知,并在API回调中发布通知。
例如:
UploadNotifications.swift
:
import Foundation
extension Notification.Name {
static let UploadProgress = Notification.Name("UploadProgress")
}
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
}
}
}
}
然后在上载进度回调的方法中:
upload.uploadProgress { progress in
NotificationCenter.default.post(name: .SyncDidProgress, object: Int64(progress))
}