与模型类中的 UIProgressBar 交互

发布于 2024-08-15 14:59:48 字数 435 浏览 3 评论 0原文

我的 Cocoa Touch 应用程序中有一个 MainViewController,它显示一个包含 UIProgressBar 视图的状态视图。

从 MainViewController 中,创建并迭代 FlickrImage 模型对象。 FlickrImage 对象本身与 Flickr API 进行交互,这需要时间,这就是我希望用户看到进度条的原因。现在的挑战(至少对我来说是这样;)是从 FlickrImage 对象与 UIProgressBar 进行交互(发送 progress 消息)。我是否可以使用 NSNotificationCenter 来执行此操作,或者我可以将 MainViewController 声明为 FlickrImage 中的前向类并直接与 UIProgressBar 交互吗?

I have a MainViewController in my Cocoa Touch app which shows a status view containing a UIProgressBar view.

From the MainViewController, FlickrImage model objects are created and iterated over. the FlickrImage objects themselves interact with the Flickr API, which takes time which is why I want the user see the progress bar. The challenge (at least to me it is ;) now is to interact with the UIProgressBar (send progress messages) from the FlickrImage objects. Do I do this using a NSNotificationCenter or can I just declare the MainViewController as a forward class in FlickrImage and interact with the UIProgressBar directly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浸婚纱 2024-08-22 14:59:48

最好的方法是让模型调用任何东西,但是使用 KVO 观察模型,然后对模型更新时收到的消息做出反应。这是因为拥有单独的模型层的要点是模型不必了解有关如何向用户呈现数据的任何信息。

因此,创建一个类来跟踪所有图像的加载(很可能您已经有这样一个类)并执行以下操作:

[imageManager addObserver:self forKeyPath:@"progress" options:nil context:nil];

确保数据管理器类具有一个名为“progress”的已声明属性(最好其值范围为 0.0 到1.0) 并且当您更新管理器中的值时,必须使用点符号: self.progress = newVal;

这比从模型类发送通知要干净得多。另一种方法是将视图注册为管理器上的委托。对于何时应该使用委托以及何时 KVO 更好,没有明确的经验法则,尽管使用委托协议的开销可能会稍微少一些。 Apple 使用这两种方法,但倾向于更多地依赖 KVO,在我看来这是一件好事。

The best way is to not have the model call anything at all, but use KVO to observe the model, and then react to the message you get when the model updates. This is because the point of having a separate model layer is that the model shouldn't have to know anything about how data is presented to the user.

So create a class that keeps track of the loading of all the images (most likely you already have such a class) and do something like:

[imageManager addObserver:self forKeyPath:@"progress" options:nil context:nil];

Make sure that the data manager class has a declared property called progress (ideally with values ranging from 0.0 to 1.0) and when you update the value in the manager, you must use the dot notation: self.progress = newVal;

This is much cleaner than sending notifications from the model class. An alternative would be to register the view as a delegate on the manager. There is no clear-cut rule of thumb for when you should use delegates and when KVO is better, although there might be slightly less overhead in using a delegate protocol. Apple uses both approaches but there is a tendency to rely more on KVO, which in my opinion is a good thing.

一口甜 2024-08-22 14:59:48

我更喜欢 NSNotificationCenter、MainViewController 注册为观察和更新 UIProgressBar。

将对象 MainViewController 保留在 FlickrImage 中并从 FlickrImage 更新 UIProgressBar,这使得从模型处理 UI(您违反了 MVC)

I prefer NSNotificationCenter, MainViewController register as observe and update the UIProgressBar.

keeping the object MainViewController in FlickrImage and updating UIProgressBar from FlickrImage which make handling UI from model(you are violating MVC)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文