views:

110

answers:

2

I have a MainViewController in my Cocoa Touch app which shows a status view containing a UIProgressBar view.

From the MainViewController, FlickrImage model objects are created and iterated over. the FlickrImage objects themselves interact with the Flickr API, which takes time which is why I want the user see the progress bar. The challenge (at least to me it is ;) now is to interact with the UIProgressBar (send progress messages) from the FlickrImage objects. Do I do this using a NSNotificationCenter or can I just declare the MainViewController as a forward class in FlickrImage and interact with the UIProgressBar directly?

A: 

I prefer NSNotificationCenter, MainViewController register as observe and update the UIProgressBar.

keeping the object MainViewController in FlickrImage and updating UIProgressBar from FlickrImage which make handling UI from model(you are violating MVC)

Girish Kolari
+1  A: 

The best way is to not have the model call anything at all, but use KVO to observe the model, and then react to the message you get when the model updates. This is because the point of having a separate model layer is that the model shouldn't have to know anything about how data is presented to the user.

So create a class that keeps track of the loading of all the images (most likely you already have such a class) and do something like:

[imageManager addObserver:self forKeyPath:@"progress" options:nil context:nil];

Make sure that the data manager class has a declared property called progress (ideally with values ranging from 0.0 to 1.0) and when you update the value in the manager, you must use the dot notation: self.progress = newVal;

This is much cleaner than sending notifications from the model class. An alternative would be to register the view as a delegate on the manager. There is no clear-cut rule of thumb for when you should use delegates and when KVO is better, although there might be slightly less overhead in using a delegate protocol. Apple uses both approaches but there is a tendency to rely more on KVO, which in my opinion is a good thing.

Felixyz
This makes sense - I must admit that I was doing the image loading in the MainViewController. I'm moving and refactoring - this is for me as much a learning excercise than anything else - and will try and report on the result. Thanks so much for your effort to explain this.
mvexel