IOS MVC - How to transfer data from a model to a controller?

I have done a lot of research on this subject, but I have a mental block on my problem. I am working on Objective-C for an iOS application

Here is my setup:

  • The view controller receives the text from the view (user input) and passes that text to the MethodA model.

  • MethodA in the model works on the input text and gets the output (for example, searches Google for this text). It searches using the dispatch_async method, which calls the MethodB selector in the model.

  • Method B parses the result and puts all the results in an NSMutableArray file

  • My question here is: how to pass this NSMutableArray to view the controller so that I can show it in the view?

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

+4
source share
5 answers

I assume that walking through a delegate object that refers to a selector method and calling this method with the processed data will be a good way to achieve the loosely coupled structure that your program deserves. Are you familiar with this concept or will I dig you some code samples?


UPDATE: Code Samples

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

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

The point is to return the result to this method when the calculation is complete. Therefore, in MyViewController , where you call MethodA , you pass a reference to the delegate to handle the result, namly self .

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

MyModel MethodA and methodB need to add the (id)delegate parameter and pass this between calls.

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

  if([delegate respondsToSelector:@selector(myCallbackMethod:)]]) [observer performSelector:@selector(myCallbackMethod:) withObject:myArray]; 
+3
source

At any time, when I want to perform asynchronous processing, and that the material should return to the interface, 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 must document in the .h file that it triggers notifications when certain events occur; for example, when part of a model has been updated. When the ViewController initializes the model object, configure it as a watcher of the documented notification and implement a callback that updates the user interface.

2 delegation and @protocol

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

+7
source

In your view, the controller:

  // your controller code ... [myModel getSearchResult:searchKeyword receiver:self action:@selector(responseReceived:)]; } - (void)responseReceived:(MyModel *)model { NSArray *searchResult = model.searchResult; [model release], model = nil; // some view actions, for instance update your table view ... } 

In your model:

 ... - (id)getSearchResult:(NSString *)searchKeyword receiver:(id)aReceiver action:(SEL)receiverAction { self.receiver = aReceiver; self.action = receiverAction; // some async actions } 

In the asynchronous response receiver:

  ... [self.receiver performSelector:self.action withObject:self]; } 
+2
source

If I do not understand your description, it looks like your "model" class does more than necessary. In this case, it does at least part of the work of your controller. My suggestion was to collapse method A and methodB into a view controller (or another controller class). Method B can still set the NSMutableArray property of the "model" instance if necessary (or skip this step if it wasn’t).

+1
source
 -(void)methodA { NSMutableArray *someArray = ... [self methodB:someArray]; } -(void)methodB:(NSMutableArray*)array { NSLog(@"%@", array); // do something with the array, like update the view } 

But if both are methods inside the view controller, why not just update the view inside the method and not pass it to another method?

0
source

All Articles