6

For instance, I have a model class which handles receiving bluetooth messages from other iPhones. 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. The view controller has a reference to the model and the view, and so can communicate to each of them.

However how should they send messages back to the VC? They could have a reference to the view controller each (as an property, with assign not retain). 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,

5
  • You could add a delegate to your model and have it point to the VC.
    – Shirkrin
    May 30, 2011 at 9:28
  • 1
    You could also post notifications using a local notificationcenter and then listen to those notifications using your VC.
    – Shirkrin
    May 30, 2011 at 9:28
  • I'll look into using a NSNotificationCenter, thank you. Do you mean create my own rather than using the default one (returned by the singleton method defaultCenter)?
    – Tom H
    May 30, 2011 at 9:37
  • 3
    No, just use the default notificationcenter - it's meant to be used :)
    – Shirkrin
    May 30, 2011 at 9:43
  • Thank you. I think I'll use that idea. However I might just listen to those messages straight from the view, or whatever makes sense in a specific scenario. (Similar to mindnoise's answer)
    – Tom H
    May 30, 2011 at 9:46

2 Answers 2

7

In general you have 3 different techniques:

  1. Delegation
  2. KVO (Key-Value Observing)
  3. Notifications

If your model only needs to inform one object (your view controller) of changes, delegation is the way to 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.

If your model needs to inform multiple objects of changes, you want to use KVO or notifications. With KVO you can subscribe to change events for a specific property or key on the model. For example, when the 'messages' property on your model changes, any attached listeners can be notified of the change and respond accordingly.

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.

So in your case, delegation or KVO would probably the best choice.

1
  • Wow, thanks for that mine of information. I had honestly never heard of KVO but I'll have a further look now at it; it might be just what I need! Thank you.
    – Tom H
    May 30, 2011 at 10:48
0

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. This is fine in my opinion, but it couples the model to the view, which is bad. So, to solve this you should implement an observer(broadcast-receive) design pattern (or use the built in ios event broadcaster /reciever system -> 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.