Howto Update a gui (progressview) from a download delegate in swift

只愿长相守 提交于 2019-12-22 10:55:05

问题


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:


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


来源:https://stackoverflow.com/questions/28601560/howto-update-a-gui-progressview-from-a-download-delegate-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!