Howto Update a gui (progressview) from a download

2019-05-10 09:36发布

I have a DownloadSessionDelegate Class to handle a downloadprocess for big files. I would like to show the progress in a progressview. The Information about the download state is in my DownloadSessionDelegate Class. Now I don't know howto update my progressview outside of that class.

Howto to do that ?

class DownloadSessionDelegate : NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {

    var handlerQueue: [String : CompleteHandlerBlock]!
...
...
...

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")

progressView.setProgress(0.5, animated: true); // <<<Howto reference the progressView
    }
}

Triggering the download from my ViewController.swift:

func download_zip(sURL: String, sToLocation: String) {


    let progressView = UIProgressView(progressViewStyle: .Bar);
    progressView.center = view.center;
    progressView.progress = 1/2;
    progressView.trackTintColor = UIColor.lightGrayColor();
    progressView.tintColor=UIColor.blueColor();
    view.addSubview(progressView);



        var delegate = DownloadSessionDelegate.sharedInstance;
        delegate.storePath=sToLocation;
        struct SessionProperties {
            static let identifier : String! = "url_session_background_download"
        }
        var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(SessionProperties.identifier)
        var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
        var url = NSURLRequest(URL: NSURL(string: sURL)!)
        var downloadTask = backgroundSession.downloadTaskWithRequest(url)
        downloadTask.resume()
    }

1条回答
Evening l夕情丶
2楼-- · 2019-05-10 10:37

To reference progress view from another class you will need to pass instance of progress view to the class that needs its reference, in your case:

class DownloadSessionDelegate : NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {

var handlerQueue: [String : CompleteHandlerBlock]!
var progressView: UIProgressView!
...
...
...

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")

    progressView.setProgress(0.5, animated: true); // <<<Howto reference the progressView
    }
}

ViewContoller

func download_zip(sURL: String, sToLocation: String) {


let progressView = UIProgressView(progressViewStyle: .Bar);
progressView.center = view.center;
progressView.progress = 1/2;
progressView.trackTintColor = UIColor.lightGrayColor();
progressView.tintColor=UIColor.blueColor();
view.addSubview(progressView);



    var delegate = DownloadSessionDelegate.sharedInstance;
    delegate.storePath=sToLocation;
    //here you pass progressView from ViewController to DownloadSessionDelegate
    delegate.progressView = progressView
    struct SessionProperties {
        static let identifier : String! = "url_session_background_download"
    }
    var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(SessionProperties.identifier)
    var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
    var url = NSURLRequest(URL: NSURL(string: sURL)!)
    var downloadTask = backgroundSession.downloadTaskWithRequest(url)
    downloadTask.resume()
}
查看更多
登录 后发表回答