Graphics Programs Reference
In-Depth Information
For a menu controller to appear, the view that is presenting the menu controller must be
the first responder of the window. So we sent the message
becomeFirstResponder
to the
TouchDrawView
before getting the menu controller. However, you might remem-
it needs to become the first responder.
In
TouchDrawView.m
, override this method to return
YES
.
- (BOOL)canBecomeFirstResponder
{
return YES;
}
You can build and run the application now, but when you select a line, the menu won't ap-
pear. This is because menu controllers are smart: When a menu controller is to be dis-
played, it goes through each menu item and asks its view if it implements the action mes-
sage for that item. If the view does not implement that method, then the menu controller
won't show the associated menu item. To get the
Delete
menu item to appear, implement
deleteLine:
in
TouchDrawView.m
.
- (void)deleteLine:(id)sender
{
// Remove the selected line from the list of completeLines
[completeLines removeObject:[self selectedLine]];
// Redraw everything
[self setNeedsDisplay];
}
Build and run the application. Draw a line, tap on it, and then select
Delete
from the menu
item. Bonus feature: the
selectedLine
property was declared as weak, so when it is
removed from the
completeLines
array,
selectedLine
is automatically set to
nil
.