簡體   English   中英

如何在swift中從下載委托更新gui(progressview)

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

我有一個DownloadSessionDelegate類來處理大文件的下載過程。 我想在進度視圖中展示進展。 有關下載狀態的信息位於我的DownloadSessionDelegate類中。 現在我不知道如何在該課程之外更新我的進度視圖。

怎么做呢?

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

從我的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()
    }

要從另一個類引用進度視圖,您需要將進度視圖的實例傳遞給需要其引用的類,在您的情況下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM