Can you refer to the sender of a message without passing the sender as a parameter? This is simplified code for the sake of discussion:
// mainTableViewController.m
[dataModel loadData]; //Table is requesting data based on user input
// dataModel.m
-(void) loadData{
// I want to store the sender for later reference
sendingTableViewController = ???? ;
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
// Web data is loaded. Ask the sending tableViewController to
// reload it's data.
[sendingTableViewController.tableView reloadData];
}
I'm still getting used to how to refer to methods and properties that are the responsibility of another object. I want to send a message to dataModel
to load some data using NSURLConnection
. But I don't just want to return the data because I don't want to sit around waiting for the data to load. I want to send a message to the mainTableViewController
once connectionDidFinishLoading
is called.
Since the loadData
method may be called from any number of tableViewControllers
I can't just say [mainTableViewController reloadData]
.
Follow-Up Question
Great Information! I love the no-judgement nature of StackOverflow.
So the mainTableViewController
would be the Delegate of the dataModel
?
Would it be correct to say that the dataModel
class defines the informal protocol?
I currently instantiate my dataModel
class from within my mainTableViewController
. So I could change my code like this:
// mainTableViewController.m
dataModel *myDataModel = [[dataModel alloc] initWithDelegate:self ];
// Does this method need to be defined in the mainTableViewController header file
// since I will already have defined it in the dataModel header file?
-(void) dataDidFinishLoading {
[self.tableView reloadData];
}
// dataModel.m
-(id) initWithDelegate:(id)aDelegate{
self.delegate = aDelegate;
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
[self.delegate dataDidFinishLoading];
}
Is it bad that my TableViewController is instantiating my dataModel, cause then my dataModel
is owned by the TableViewController? Should I really instantiate the dataModel
from the AppDelegate instead?
Thank You!