Imagine you have a controller that has two table views:
@interface MyController : NSViewController <NSTableViewDelegate>
@property(nonatomic,weak) IBOutlet NSTableView *tableView1;
@property(nonatomic,weak) IBOutlet NSTableView *tableView2;
@end
And controller is set as delegate for both table views. Now you want to track changes in selections of both tables. You implement tableViewSelectionDidChange:
delegate method, but when it is called, how do you know which table view did change it's selection? Here comes the notification
argument. It's object
property points to table view that did sent this notification, and you can easily differentiate between two. Also it may contain userInfo
dictionary, for example NSTableViewColumnDidResizeNotification
contains a NSTableColumn
and NSOldWidth
keys in there.
In described case, the method responding to notification was called under delegate idiom, but in other scenarios (observed notification, target-action, etc.) you also sometimes must differentiate objects that caused a method call.
@implementation MyController
...
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
if ([notification object] == self.tableView1)
NSLog(@"selection changed in table 1");
else if ([notification object] == self.tableView2)
NSLog(@"selection changed in table 2");
else
NSLog(@"selection changed in unknown table (???)");
}
- (void)buttonDidClick:(id)sender
{
NSLog(@"some button did click. which? %@", sender);
}
@end