如何在swift中从下载委托更新一个gui(progressview)
我有一个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() }
- iOS推送通知自定义格式
- iOS-Swift,Stripe createEphemeralKeys返回Google SignIn页面
- 在SceneKit中,SCNLightTypeAmbienttypes的SCNLight是否忽略categoryBitMask参数?
- 根据设备(iPhone或iPad)的不同设备方向
- iOS – 在codeSense中出现自动合成属性的“添加”方法
- 为iOS提供自动续订订阅
- iOS:色彩究竟干什么?
- 当hidesBottomBarWhenPushed仅在iOS7上设置为YES时,UITabBarController子视图消失
- NSDateFormatter.dateFromString返回特定语言的特定date为零