模型如何与视图控制器对话?
这是我无法开始工作的事情......我可以让视图控制器与我的自定义对象很好地对话......但是如何从我的对象向视图控制器发送消息?
从 myViewController 到 myObject 的消息看起来像 [myObject doSomething]。
相反的消息会是什么样子?以其他方式发送消息是否有意义?
非常感谢您的帮助!
Here is something I just can't get to work....I can make the view controller talk to my custom objects just fine....but how do I send messages to the View Controller from my objects?
A message from myViewController to myObject would look like
[myObject doSomething].
What would the opposite message look like? Does it even make sense to send a message the other way?
Greatly appreciate your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将控制器传递给模型,但通常您希望模型不依赖于视图或控制器。
为了避免这种情况,请创建一个模型想要与之通信的协议,并让视图控制器实现它,并让模型将协议的实例而不是视图控制器作为属性。
You could pass a controller to a model, but often you want models to not depend on views or controllers.
To avoid that, make a protocol that the model wants to talk to and have the view controller implement it and have the model take an instance of the protocol, not the view controller as a property.
我经常使用 NSNotificationCenter 将模型对象的更新广播到感兴趣的控制器。为了更紧密地绑定交互,请考虑为模型对象制定委托协议。
尽管侦听器可以访问发送通知的模型对象,但通知主要是一种方式。可以有任意数量的相关方,如果控制器来来去去但模型是持久的,则包括没有相关方。
委托有两种方式,但一次只能有一名委托。通常,委托的寿命应该比它所委托的对象的寿命长。委托可能适合模型对象生命周期中需要额外用户输入的阶段。
通知和委托可以同时使用。与 UIApplication 一样,通常在发送通知之前调用委托。
I often use NSNotificationCenter to broadcast updates from model objects to interested controllers. For a more tightly bound interaction, consider making a delegate protocol for the model object.
A notification is mostly one way, although the listener could access the model object that sent the notification. There can be any number of interested parties, including none if controllers come and go but the model is persistent.
A delegate is two way, but there can only be one delegate at a time. Usually a delegate is expected to outlive the object it is a delegate to. A delegate might be good for a phase of a model object lifespan that requires extra user input.
Notifications and delegates can be used simultaneously. As with UIApplication, the delegate is usually called before the notification is sent.
为什么您希望您的模型首先主动与任何东西对话?视图控制器是应用程序流的主动管理者,并发起与模型的通信,而不是相反。
您能举一个更具体的例子来说明您实际上需要这样做的情况吗?
就像您怀疑自己一样,大多数时候“以其他方式”发送消息是没有意义的。但如果您确实需要这样做,从模型“广播”信息的适当方法是 通知。 您可以让模型发送通知,并且视图控制器或任何其他对象可以订阅这些通知(如果他们关心),但没有严格的限制从模型到其他应用程序的耦合。
Why would you want your model to actively talk to anything in the first place? View controllers are the active managers of the app flow, and initiate communications to the model, not the other way around.
Can you say a more specific example of a situation where you would actually need to do this?
Like you suspect yourself, most of the time it doesn't make sense to send messages “the other way.” But if you really need to do that, an appropriate way to broadcast information “out” from models is notifications. You can have your model send notifications, and view controllers or any other objects can subscribe to those notifications if they care, but there is no tight coupling from model to other app pieces.