Betan
Betan

Reputation: 3

Objective-C: How to communicate between classes?

after an research i discovered the sigleton that may help me but i have some problem to understand how it work. i need that 2 class comunicate each other, here an example: i have a tableView wich field are dinamic. this is my code on viewController.h file:

    -(void)ottieniMarche{

    responseSimulate = [[NSArray alloc]initWithObjects:@"pollo",@"cane",@"gatto",@"verme",@"gallo",@"topo",@"canguro",@"elefante",@"giraffa" ,nil];

}

Now i have to send this information on my mainTableView.m ad the code that i'm actually using is this:

    - (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization

        elementMainTableView = [[NSArray alloc]initWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",@"eee",@"fff",@"ggg",@"hhh",@"iii" ,nil];

    }
    return self;
}

Now i have to change the elementMainTableView value with the responseSimulate value. I mean that the content of my maintableView are the same of my responseSimulate.

Upvotes: 0

Views: 360

Answers (1)

FluffulousChimp
FluffulousChimp

Reputation: 9185

If I understand your question correctly, you want to refresh the content of the UITableView based on model changes.

UITableView requires a class that conforms to the UITableViewDataSource protocol to provide its row and section data. Often, that's a UITableViewController, but it does not have to be. The data source for your UITableView could the other class to which you refer. In that case, the key is to ask the reload the data, i.e. [tableView reloadData] when you change the data.

In your case, if your MainTableView (I'm inferring this is actually a UITableViewController subclass...) conforms to the UITableViewDataSource protocol, then you could solve the problem using Key-Value Observing for example: (Note, this example assumes you are using ARC.)

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if( !self ) return nil;

    elementMainTableView = [[NSArray alloc] initWithObjects:@"aaa",@"bbb",@"ccc",nil];
    [self addObserver:self forKeyPath:@"elementMainTableView" options:NSKeyValueObservingOptionNew context:NULL];
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
    if( [keyPath isEqualToString:@"elementMainTableview"] )
    {
        [[self tableView] reloadData];
    }
}

This assumes that you expose elementMainTableView as a property on MainTableView.

Now, in the other class (?ViewController) your method:

-(void)ottieniMarche {
    responseStimulate = [[NSArray alloc] initWithObjects:@"pollo",@"cane",@"gato",nil];
    _mainTableViewController.elementMainTableView = responseStimulate;
}

For this to work, you will need your ViewController to keep a reference to the MainTableView, e.g. _mainTableViewController above.

Upvotes: 1

Related Questions