在遵守 MVC 约定的同时实现 iPhone Store Kit

发布于 2024-08-08 17:02:52 字数 278 浏览 3 评论 0原文

我正在尝试在实施 Store Kit 时提出良好的设计。

尝试遵循 MVC 约定,我有以下

模型:Store Observer View:一些将显示UI(产品选择等)的View 控制器:我的视图控制器

我的视图控制器是否应该为产品请求实例化一个 Store Observer 类并更新 UI。这是这样做的正确方法吗?

由于它是异步的,我也无法确定在哪里进行产品请求调用。以及在获取产品信息后我如何更新 UI(我是否调用 SetNeedsDisplay)

非常感谢

I am trying to come up with good design while implementing Store Kit.

Trying to follow MVC convention, I have the following

Model: Store Observer
View: Some View that will display UI (product selection, etc)
Controller: My View Controller

Should my View Controller instantiate a Store Observer Class for product request and update the UI. Is this the proper way of doing this?

I am also having trouble determining where to make the Product Request calls since it is ASYNC. And how I go about updating the UI after the the product Info is fetched (do I call SetNeedsDisplay)

Thank you very much

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

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

发布评论

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

评论(1

離殇 2024-08-15 17:02:52

您应该调查代表。通过定义代理对象所遵循的委托协议,您可以在不破坏 MVC 的情况下使事情发生。然后,您可以通过调用委托协议中定义的方法来调用委托方法。效果有点像有一个指向对象的指针并直接调用其方法之一,但它不一样 - 对象仍然是解耦的,并且通过定义协议,您可以定义可以执行的操作,这样您就不会结束课程无可救药地交织在一起。

引起该操作的对象(称之为 A)定义了一个委托属性。它被设置为某个对象 B,符合委托协议。 B 需要在其标头中包含委托说明符以表明它符合协议,并且需要实现协议中定义的每个方法(定义为可选的方法除外)。只要协议公开了方法,A 就可以调用 B 中的方法。您最终不会得到 A 有一个指向 B 的指针,而 b 有一个指向 A 的指针。这正是 UITableView 的工作原理,您可能已经使用了委托。

在您的情况下,看起来控制器将创建一个商店观察者。它还将为模型必须发送回控制器的任何通知实现委托方法。模型的委托设置为控制器,并且控制器可以收到模型更改(它可以正确访问模型)的通知,以便更新 UI。

You should look into Delegates. You can cause things to happen without breaking MVC by defining a delegate protocol which the acting object conforms to. You can then call a delegate method by a call to the method defined in the delegate protocol. The effect is kind of like having a pointer to the object and calling one of its methods directly but it's not the same - the objects are still decoupled and by defining the protocol you define just what you can do so that you don't end up with classes hopelessly intertwined.

The object causing the action - call it A - defines a delegate property. It is set to some object, B, conforming to the delegate protocol. B needs to include the delegate specifier in its header to indicate that it conforms to the protocol, and it needs to implement every method defined in the protocol (except those defined as optional). A can then call methods in B as long as the protocol exposes them. You don't end up with A having a pointer to B and b having a pointer to A. This is exactly how a UITableView works, you have probably used delegates already.

In your case, looks like the controller will create a store observer. It will also implement delegate methods for any notifications the model must send back to the controller. The model's delegate is set to the controller and the controller can be notified of changes to the model (which it correctly has access to) in order to update UI.

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