简体   繁体   English

model 与视图 controller 通信的推荐方式是什么?

[英]What is the recommended way for a model to communicate with a view controller?

For instance, I have a model class which handles receiving bluetooth messages from other iPhones.例如,我有一个 model class 处理从其他 iPhone 接收蓝牙消息。 When I receive one of these messages, I need to update a view.当我收到其中一条消息时,我需要更新视图。 I believe the standard way of doing this is through the view controller.我相信这样做的标准方法是通过视图 controller。 The view controller has a reference to the model and the view, and so can communicate to each of them.视图 controller 引用了 model 和视图,因此可以与它们中的每一个进行通信。

However how should they send messages back to the VC?但是他们应该如何将消息发送回 VC? They could have a reference to the view controller each (as an property, with assign not retain).他们可以参考 controller 每个视图(作为属性,分配不保留)。 Is that bad practise (if I'm not mistaken its a circular reference)?这是不好的做法(如果我没记错它是循环引用)?
Are there alternate ways of doing this?有没有其他方法可以做到这一点? I've considered the delegate pattern, but to write an entire delegate and all seems like quite a lot of work for a simple problem.我已经考虑过委托模式,但是要编写一个完整的委托,对于一个简单的问题来说似乎需要做很多工作。 Alternatively, if you think I'm overthinking this, feel free to tell me!或者,如果您认为我想太多了,请随时告诉我!

[I think this question has probably come up before, it seems quite common, but I searched for a bit and didn't find much] 【我觉得这个问题之前大概也出现过,好像挺常见的,但是我搜索了一下,没找到太多】

Thanks for your help,谢谢你的帮助,

In general you have 3 different techniques:一般来说,您有 3 种不同的技术:

  1. Delegation代表团
  2. KVO (Key-Value Observing) KVO(键值观察)
  3. Notifications通知

If your model only needs to inform one object (your view controller) of changes, delegation is the way to go.如果您的 model 只需要通知一个 object(您的视图控制器)更改,则委托是 go 的方式。 It may feel like extra work to create a new interface, add the delegate property to the model, etc. but it is definitely worth it in terms of flexibility, code reuse, design, etc. Delegation is a standard pattern in Cocoa programming and is used extensively in Apple's APIs.创建新接口、将委托属性添加到 model 等可能感觉像是额外的工作,但在灵活性、代码重用、设计等方面绝对值得。委托是 Cocoa 编程中的标准模式,并且是在 Apple 的 API 中广泛使用。

If your model needs to inform multiple objects of changes, you want to use KVO or notifications.如果您的 model 需要通知多个对象更改,您想使用 KVO 或通知。 With KVO you can subscribe to change events for a specific property or key on the model.使用 KVO,您可以订阅 model 上特定属性或键的更改事件。 For example, when the 'messages' property on your model changes, any attached listeners can be notified of the change and respond accordingly.例如,当 model 上的“消息”属性发生更改时,任何附加的侦听器都可以收到更改通知并做出相应的响应。

Notifications are used when you want to send application-wide messages to multiple listeners.当您想要将应用程序范围的消息发送到多个侦听器时,将使用通知。 Examples from the standard APIs are keyboard notifications (when the keyboard is displayed/dismissed), and interface orientation changes.标准 API 的示例是键盘通知(当键盘显示/关闭时)和界面方向更改。

So in your case, delegation or KVO would probably the best choice.因此,在您的情况下,委托或 KVO 可能是最佳选择。

Never did this in an iOS application, but in general mvc terms, sometimes it makes better sense (and keeps the code cleaner) to update the view directly from the model yes.在 iOS 应用程序中从未这样做过,但在一般 mvc 术语中,有时直接从 model 更新视图更有意义(并保持代码更清晰)是的。 This is fine in my opinion, but it couples the model to the view, which is bad.在我看来这很好,但是它将 model 与视图耦合在一起,这很糟糕。 So, to solve this you should implement an observer(broadcast-receive) design pattern (or use the built in ios event broadcaster /reciever system -> NSNotificationCenter).因此,要解决这个问题,您应该实现一个观察者(广播接收)设计模式(或使用内置的 ios 事件广播器/接收器系统 -> NSNotificationCenter)。 This way, when something happens that changes the model, the model will broadcast an even, whether someone listens to that event or not, it's not its problem anymore, and so, you decouple the view from the model.这样,当发生改变 model 的事情时,model 将广播一个偶数,无论是否有人收听该事件,这不再是它的问题了,因此,您将视图与 Z20F35E630DAF44D88C3F6 分离。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 视图控制器之间通信的最佳方式是什么? - What's the best way to communicate between view controllers? 如何与不同的视图控制器通信? - How to communicate with different view controller? 通过presentModalViewController添加的视图将其解雇与调用该调用的类进行通信的最佳方法是什么? - What is the best way for a view added via presentModalViewController to communicate its dismissal with the class which invoked the call? 模型视图控制器问题 - model view controller question 表视图控制器如何与其详细视图进行通信? - How does a table view controller communicate with its detail view? UITableViewCell中的自定义附件视图向表视图控制器发送消息的好方法是什么? - What is a good way for a custom accessory view in a UITableViewCell to message the table view controller? 将视图添加到视图控制器并使用“自动版图”通过旋转使其完全覆盖视图的正确方法是什么? - What is the proper way to add a view to a view controller and make it cover it completely through rotations using Auto Layout? 在父视图控制器和子视图控制器之间进行通信的更好方法是什么? - What is the better approach to communicate between parent and child view controllers? 父视图控制器和模型视图控制器解雇检测不起作用 - Parent View Controller and Model View Controller dismissal detection not working 区分进入同一视图控制器的两种方法的最佳方法是什么? - What is the best way to differentiate between two methods going to same view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM