views:

38

answers:

1

I'm confused as to what the first part of the following function declaration means. It adds an object instance as part of the function definition.

E.g. In some sample code, class ItemsViewController which derives from UITableViewController has this function definition:

-(void) tableView:(UITableView*) aTableView didSelectRowAtIndexPath:(NSIndexPath*) indexPath
{ ... }

What exactly does the tableView:(UITableView*) aTableView bit achieve?

+6  A: 

It allows your delegate to serve as the delegate for multiple UITableViews. When an event happens on any of the UITableViews, the appropriate delegate method gets called and you can use the first parameter to determine which UITableView the event relates to, and act accordingly. (Of course, your delegate has to have some way of knowing which view is which, for instance by having outlets to each of the views that it's the delegate for.)

David
David's got it.
skantner
Thanks - still need a bit of clarification because its still not obvious to me1. So does the 'tableView' part tie the delegate only to the tableView instance?2. Is aTableView considered a parameter to the function?
Giablo
Ah, I see what you're missing now. Method names in Objective-C have *named parameters* ; each parameter has a little tag before it, then a `:`, then the type name in parentheses, and then the actual name of the variable that gets used to refer to the parameter from inside the method. The idea is that method calls should look like English sentences. So, the parameters to the above method are `aTableView` and `indexPath`. You can use a single object as the delegate for any number of things (or even multiple similar things, e.g. multiple UITableViews) but you must provide all the required methods
David