从swift中的单独类访问label.text [英] Access label.text from separate class in swift

查看:132
本文介绍了从swift中的单独类访问label.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的viewController,允许用户从网址下载文件。
为了保持我的代码清洁,我创建了一个从我的视图中调用的下载类。它工作正常但现在我想为用户提供一些关于下载的UI提示。

I have a viewController with a button allowing user to download a file from a url. To keep my code clean I created a download class that I call from my view. It works fine but now I want to provide users with some UI cues about the download.

假设我有一个标签,我想将其文本更改为已下载一旦下载结束。应该怎么做?

Let say I have a label and I want to change its text to "Downloaded" once the download is over. How one should do that?

这里是我的代码:

FileViewController

FileViewController

@IBOutlet weak var downloadLbl: UILabel!

func downloadFile(sender:UIButton!)
    {      
        fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
    }

(这只是重要部分)

然后我将我的下载功能放在一个单独的类文件中,然后启动一个DownloadTaskSession并使用以下委托来观察下载完成

then I have my download function inside a separate class file and I start a DownloadTaskSession and I use the following delegate to observe download completion

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
        println("session \(session) has finished the download task \(downloadTask) of URL \(location).")

        FileViewController().downloadLbl.text = "downloaded"
    }

最后一行: FileViewController()。downloadLbl.text =已下载返回错误:致命错误:在解包可选值时意外发现nil 。

the last line: FileViewController().downloadLbl.text = "downloaded" returns an error: fatal error: unexpectedly found nil while unwrapping an Optional value.

有人可以提供帮助吗?

结论

两个建议的解决方案在创建通信解决方案之间的沟通时我正在工作,但我去了通知解决方案,因为在我的情况下,我必须更新UI以及后台线程进展和使用协议不是按预期工作。进行了通信,但即使在使用dispatch_async方法推回主线程上的更改后,更新UI仍无法正常工作。

Both proposed solution are working when it comes to create communication between classed but I went for the notification solution because in my case I had to update UI along with background thread progression and using protocol wasn't quite working as expected. The communication was made but updating the UI wasn't working all the time, even after using dispatch_async method to push back the changes on the main thread.

使用通知系统帮助,因为它更容易实现,管理和使用UI修改。

Using the notification system helped as it was easier to implement, managed and use for UI Modifications.

推荐答案

FileViewController().downloadLbl.text = "downloaded"

表示创建新的FileViewController,并设置downloadLbl文本。

is means "create new FileViewController, and set downloadLbl text".

您需要将文本设置为现有的FileViewController。

为了将来自URLSession完成块的消息告知FileViewController,您可以使用委托或通知模式。

You need to set text to existing FileViewController.
For telling message from URLSession completion blocks to FileViewController, you use delegate or notification pattern.

例如,通知模式:

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
    println("session \(session) has finished the download task \(downloadTask) of URL \(location).")

    // notify download complete!
    let defaultCenter = NSNotificationCenter.defaultCenter()
    defaultCenter.postNotificationName("CompleteDownloadNotification",
    object: nil,
    userInfo: nil)
}

----------------

@IBOutlet weak var downloadLbl: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // ready for receiving notification
    let defaultCenter = NSNotificationCenter.defaultCenter()
    defaultCenter.addObserver(self,
        selector: "handleCompleteDownload",
        name: "CompleteDownloadNotification",
        object: nil)
}

func handleCompleteDownload() {
    // if notification received, change label value
    downloadLbl.text = "downaloded"
}

func downloadFile(sender:UIButton!) {      
    fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
}

更多细节: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/

快乐编码:)

这篇关于从swift中的单独类访问label.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆