Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS MVC - How to pass data from model to controller?

I did quite a bit of research on this, but I am having a mental block about my problem. I am working on Objective-C for an iOS app

Here's my set up:

  1. The view controller gets a text from the view (user input), and passes that text to the MethodA of the model.

  2. The MethodA in model works on the input text and gets an output (e.g. searches google for that text). It does the search using dispatch_async method which calls the selector to MethodB within model.

  3. MethodB parses the output and puts all the results into a NSMutableArray

  4. My Question Here: how do I pass that NSMutableArray back to view controller so I can show it on the view?

Sorry, if the answer to my question is very simple/obvious. I am new to Objective-C

like image 447
BlueChips23 Avatar asked Dec 12 '22 00:12

BlueChips23


2 Answers

Any time I want to do async processing and that stuff needs to get back into the UI somewhere, I do one of two things:

1. Use NSNotification to tell anyone who cares that the work is complete
2. Use a delegate property on the worker and a @protocol

1 NSNotification

The model object should document in it's .h file that it fires notifications when certain things happen; such as when a portion of the model has been updated. When the ViewController initializes the model object, have it set itself up as an observer of the documented notification, and implement a callback which updates the UI.

2 delegation and @protocol

Create a @protocol such as @protocol FooModelUpdateDelegate with a method properly named such as fooObjectDidUpdate:(Foo *)object;, and then the model class has a delegate property as id<FooModelUpdateDelegate> updateDelegate and the ViewController sets itself as that delegate, and I'm sure you can figure out the rest.

like image 120
Chris Trahey Avatar answered Jan 08 '23 16:01

Chris Trahey


I guess passing along a delegate-object that respoons to a selector-method and calling this method with the processed data will be a good way to achieve the loosley coupled structure your program deserves. Are you familiar with this concept, or shall I dig up some code-samples for you?


UPDATE: Code samples

So, I would probably use the calling class, say MyViewController, to implement the callbackMethod, myCallbackMethod as follows:

-(void) myCallbakcMethod: NSMutableArray* array {
    //DoWhatever with the returned data
}

The point is to get the result passed back to this method when the computation is finished. So in your MyViewController where you call MethodA you pass along a reference to the delegate to handle the result, namly self.

//From this:
[MyModel methodA:param1]
//To:
[MyModel methodA:param1:self]

MyModels methodA and methodB would need to add a parameter (id)delegate and pass that along between the calls.

In methodB where the data myArray is ready, do the following call:

    if([delegate respondsToSelector:@selector(myCallbackMethod:)]])
        [observer performSelector:@selector(myCallbackMethod:) withObject:myArray];
like image 24
mariusnn Avatar answered Jan 08 '23 15:01

mariusnn