简体   繁体   English

如何更新另一个UIViewController-SWIFT上的进度视图?

[英]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.

相关问题 如何快速将视图设置为UIViewController的最大范围? - How to set a view to the max bounds of the UIViewController in swift? 如何将UIViewController传递给Swift中的另一个类? - How to pass UIViewController to another class in Swift? 如何从 swift 5 中的另一个 Controller 重新加载 UIViewController - How to reload UIViewController from another Controller in swift 5 如何在另一个UIViewController中快速更改NSIndexPath - How to change NSIndexPath in another UIViewController in swift Swift 2-为什么UIButton在视图中无法正常工作,因此又添加为另一个UIViewController的子视图? - Swift 2 - Why is a UIButton not working in a view added as a child in another UIViewController? 在另一个viewController中的自定义单元中更新进度视图 - Update progress view in custom cell in another viewController 使用Swift Programmaticaly将UIViewController与另一个UIViewController重叠 - Overlapping UIViewController with another UIViewController Using Swift Programmaticaly 如何在moya swift中添加进度视图? - how to add progress view in moya swift? swift 如何在多个 ViewController 中查看 downloadTask 的进度 - swift How to view progress of downloadTask in several ViewControllers 快速改变uiviewcontroller的观点 - swift change uiviewcontroller's view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM