Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing the MVC pattern

I have a problem to implementing the MVC pattern in iOS with Swift. According to the Apple documentation, there is a MVC schema:

MVC pattern

I am fine with that but as you can see, when the model changes itself (an incoming message from a socket, for exemple) how is it supposed to notify the controller?

For instance, I have a chat application with a model that represents a list of messages. When the model receives a new message, how does it notify the controller? Is there a conventional way to do that?

Thanks

like image 425
Mathieu Avatar asked May 31 '16 20:05

Mathieu


1 Answers

You can achieve this Model - Controller communication in two ways.

  1. Delegate pattern
  2. Notifications (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/index.html)

For detailed explanation I would recommend you to watch CS 193p MVC lecture. (https://www.youtube.com/watch?v=Cb8KtEI3ZaY)

like image 73
slonkar Avatar answered Oct 25 '22 14:10

slonkar