-1

I have three UIViewController that call a method in another Class, when there is need.

This fourth Class subclass of NSObject, which I will call CheckController, it makes a connection to a database on a server, and to do this takes a few seconds.

I have two questions to ask:

-FIRST: now to get the array back to me from the server, use a method that is almost embarrassing for me to share (bad code for me), so i need to know how to call this CheckController and return a value in the ViewController from which the call originated.

-SECOND: how do I know in CheckController, from which UIViewController, the method was invoked?

this is code of CheckController:

 -(void)connectionWithString:(NSString *)string {

   //connection with server - work well

  }

  ...

  -(void)connectionDidFinishLoading:(NSURLConnection *)connection {

     ...

    [self returnArray:myObject];

  }

  -(void)returnArray:(NSMutableArray *)arrayReturn {
   //in this method i set the BOOL done to YES, but i believe that is it possible to
   //send directly this arrayReturn to ViewController that invoked this method

   done = YES;
   NSLog(@"arrayReturn = %@", arrayReturn);
  }

thanks in advance for the help, and tell me if something is not clear

Ilario
  • 5,979
  • 2
  • 32
  • 46
  • @downvoter thanks for the negative vote, now that you're happy to have voted negatively, Can you explain why? :-) – Ilario Jan 13 '14 at 16:52
  • I would create a class specifically for interfacing with your database and build delegate methods for it that provide notification when a value has been updated. – sosborn Jan 13 '14 at 16:57
  • @sosborn can you show me how? CheckController is a subclass of NSObject and was created just to make this connection – Ilario Jan 13 '14 at 16:59
  • You said that checkController is a UIViewController. Regardless, look at the docs for the Delegate pattern then read this StackOverflow answer: http://stackoverflow.com/questions/645449/how-to-use-custom-delegates-in-objective-c – sosborn Jan 13 '14 at 17:09
  • @sosborn i edited my answer, go to read that link – Ilario Jan 13 '14 at 17:11
  • First off, forget about "delegates". Depending on precisely what you're doing you *may* want to use a delegate pattern, but it's really just an idiom superimposed over normal programming practices -- nothing magical. It's convenient for extensibility but not necessary for basic function. – Hot Licks Jan 13 '14 at 17:13
  • (And probably the simplest way to handle your async function is to have the "caller" listen for a "notification" from connectionDidFinishLoading. Not as elegant as using blocks and such, but easy to understand.) – Hot Licks Jan 13 '14 at 17:16
  • @HotLicks so if i understand, you suggest to use NSNotification? – Ilario Jan 13 '14 at 17:22
  • NSNotificationCenter -- `addObserver:selector:name:object:` (or you can use the block version if you must) and `postNotificationName:object:userInfo:`. The `userInfo` is your returned data. – Hot Licks Jan 13 '14 at 17:30
  • @HotLicks thank you very much for your suggestion, i tried the answer of Fabian, and its work ;-) – Ilario Jan 13 '14 at 17:35

1 Answers1

1

As you already mentioned your Class is a subclass of NSObject which is good. But you should provide a protocol which calls the delegate if your CheckController receives some data. So in your CheckController.h

@protocol CheckControllerDelegate <NSObject>
@required
- (void)receivedArray:(NSArray*)data
@end

@property (nonatomic, weak) id <CheckControllerDelegate> delegate;

In your ViewController:

[self.CheckController setDelegate:self];

CheckController.m:

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
     [_delegate receivedArray:myObject];
 }

For best practice your protocol should also have a method which would be called if an error occurs.