2

I am trying to call a method thats in my ViewController from a NSObject Class thats doing some parsing.

I initally call a connection class I have made wich downloads some data from my server, I then pass this data over to a parser class I have made, now what I am trying to do is pass this data back to the viewcontroller and reload the tableview thats in this view (thats on a navigation stack)

anyway this is causing some errors and I think it might be the way I am trying to call this method thats doing it. here is how I call it.

MyViewController *myViewController = [[MyViewController alloc] init];
    [myViewController initFilterArray:filteredArray];

Now I think this is causing an issue because I am allocating a new viewcontroller object? is that right.. not to sure of the terminoligy.. but yea..

the result of which is that reloaddata is only calling

  • numberOfSectionsInTableView
  • tableView:numberOfRowsInSection

then thats it. any help would be appreciated.

UPDATE: so I am trying to set up a protocol/delegate to see if that fixes my problem.

so in my class.h this is what I am doing

@protocol PassParsedData <NSObject>
@required
- (void) sendMyArray:(NSArray *)modelArray;
@end
//..
id <PassParsedData> delegate;
//..
@property (strong) id delegate;

then in class.m

//..method
[[self delegate]sendMyArray:filteredArray];
//..

so thats my class, then over in my view controller where I want to call this sendMyArray I do this

viewcontroller.h

#import "class.h" //delegates & protocols
//..
interface VehicleSearchViewController : UITableViewController <PassParsedData> {
//..

then i call it like this

viewcontroller.m //..

- (void)sendArray:(NSArray *)array
{
    ICMfgFilterArray = array;
    [self.tableView reloadData]; 
}
C.Johns
  • 10,185
  • 20
  • 102
  • 156

1 Answers1

1

One way of doing this would be the recommended approach of delegates and protocols. Your NSObject declares a protocol. The ViewController actually implements the protocol and sets itself as the delegate. Then the NSObject calls the method (not knowing who implements it). It is a loosely-coupled way to communicate between objects.

I actually recently wrote a blog post on a basic introduction to protocols and delegates if you're interested...

UPDATE

Based on your update above in question. Don't forget to set your ViewController to be the delegate.

- (void)viewDidLoad {
        // State that you will take care of messages from graphView (provided you have the      protocol implementation!)
        self.yourClass.delegate = self;
    }

And the method in your ViewController should match the protocol signature. So in ViewController.m

- (void) sendMyArray:(NSArray *)modelArray {
    ICMfgFilterArray = array;
    [self.tableView reloadData]; 
}
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • cool.. I have actually just started doing this now.. I have used this between viewcontrollers in the past but am now having some issues using it between nsobjectclass and viewcontroller.. will read your blog now.. thanks. – C.Johns Mar 12 '12 at 21:48
  • okay.. think I pretty much have it set atm. But when I call my protocol that I have set up in my class its never accessing it from the viewcontroller.. I am going to update my question above with everything I have done, but will do that after lunch as I am starving :P would be awesome if you could check over how im setting up the protocol/delegate.. – C.Johns Mar 12 '12 at 22:41
  • thanks.. just tried using self.myClass.delegate = self; and its giving an error that says - proerty 'myClass' not found an object of type viewController – C.Johns Mar 12 '12 at 23:43
  • Okay, so you need to hook up the class to the controller i.e. have the class as a property on the controller, for example. Or have another object set the view controller to be the delegate for the class. Something needs to set that. Depends how your system is designed. – Peter Kelly Mar 13 '12 at 00:26
  • is that not the same thing is adding the parameter into the interface of the .h like so **interface VehicleSearchViewController : UITableViewController {** – C.Johns Mar 13 '12 at 00:37
  • okay got the error to go away, I set it up like this EngineResponses *engineResponses = [[EngineResponses alloc] init]; [engineResponses setDelegate:self]; but its still not getting the array in the ViewController – C.Johns Mar 13 '12 at 01:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8815/discussion-between-peter-kelly-and-c-johns) – Peter Kelly Mar 13 '12 at 08:56