Graphics Programs Reference
In-Depth Information
NSString *selector = NSStringFromSelector(_cmd);
// selector is now "showImage:atIndexPath:"
selector = [selector stringByAppendingString:@"atIndexPath:"];
// Prepare a selector from this string
SEL newSelector = NSSelectorFromString(selector);
NSIndexPath *indexPath = [[self tableView] indexPathForCell:self];
[[self controller] showImage:sender
atIndexPath:indexPath];
// Ignore warning for this line - may or may not appear, doesn't matter
[[self controller] performSelector:newSelector
withObject:sender
withObject:indexPath];
}
(As you might expect, there's also performSelector: for messages with no argu-
ments, performSelector:withObject: for messages with one argument, and so
on for all the possible message sends.)
At this point, we hope your Objective-C programmer warning sirens are going off. Are
you thinking, “If we can just invent messages to send, then isn't it too easy to send a mes-
sage that an object won't respond to?” With great power comes great responsibility. Only
you can prevent unrecognized selector exceptions by checking with the receiver first.
In HomepwnerItemCell.m , check to see if the controller implements
showImage:atIndexPath: first.
- (IBAction)showImage:(id)sender
{
NSString *selector = NSStringFromSelector(_cmd);
selector = [selector stringByAppendingString:@"atIndexPath:"];
SEL newSelector = NSSelectorFromString(selector);
NSIndexPath *indexPath = [[self tableView] indexPathForCell:self];
if (indexPath) {
if ([[self controller] respondsToSelector:newSelector]) {
// Ignore warning for this line - may or may not appear, doesn't matter
[[self controller] performSelector:newSelector
withObject:sender
withObject:indexPath];
}
}
}
Let's verify this all works according to plan. In ItemsViewController.m , imple-
ment showImage:atIndexPath: to print out the index path.
- (void)showImage:(id)sender atIndexPath:(NSIndexPath *)ip
{
 
Search WWH ::




Custom Search