本文共 3547 字,大约阅读时间需要 11 分钟。
Adding Custom Items to the Edit Menu You can add a custom item to the edit menu used for showing the system commands Copy, Cut, Paste, Select, Select All, and Delete. When users tap this item, a command is issued that affects the current target in an application-specific way. The UIKit framework accomplishes this through the target-action mechanism. The tap of an item results in an action message being sent to the first object in the responder chain that can handle the message. Figure 6-1 shows an example of a custom menu item (“Change Color”). An instance of the UIMenuItem class represents a custom menu item. UIMenuItem objects have two properties, a title and an action selector, which you can change at any time. To implement a custom menu item, you must initialize a UIMenuItem instance with these properties, add the instance to the menu controller’s array of custom menu items, and then implement the action method for handling the command in the appropriate responder subclass. Other aspects of implementing a custom menu item are common to all code that uses the singleton UIMenuController object. In a custom or overridden view, you set the view to be the first responder, get the shared menu controller, set a target rectangle, and then display the edit menu with a call to setMenuVisible:animated:. The simple example in Listing 6-3 adds a custom menu item for changing a custom view’s color between red and black. Listing 6-3 Implementing a Change Color menu item - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {} - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *theTouch = [touches anyObject]; if ([theTouch tapCount] == 2) { [self becomeFirstResponder]; UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)]; UIMenuController *menuCont = [UIMenuController sharedMenuController]; [menuCont setTargetRect:self.frame inView:self.superview]; menuCont.arrowDirection = UIMenuControllerArrowLeft; menuCont.menuItems = [NSArray arrayWithObject:menuItem]; [menuCont setMenuVisible:YES animated:YES]; } } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {} - (BOOL)canBecomeFirstResponder { return YES; } - (void)changeColor:(id)sender { if ([self.viewColor isEqual:[UIColor blackColor]]) { self.viewColor = [UIColor redColor]; } else { self.viewColor = [UIColor blackColor]; } [self setNeedsDisplay]; } Note: The arrowDirection property of UIMenuController, shown in Listing 6-3, is new in iOS 3.2. It allows you to specify the direction the arrow attached to the edit menu points at its target rectangle. Also new is the Delete menu command; if users tap this menu command, the delete: method implemented by an object in the responder chain (if any) is invoked. The delete: method is declared in the UIResponderStandardEditActions informal protocol. Dismissing the Edit Menu When your implementation of a system or custom command returns, the edit menu is automatically hidden. You can keep the menu visible with the following line of code: [UIMenuController setMenuController].menuVisible = YES; The system may hide the edit menu at any time. For example, it hides the menu when an alert is displayed or the user taps in another area of the screen. If you have state or a display that depends on whether the edit menu is visible, you should listen for the notification named UIMenuControllerWillHideMenuNotification and take an appropriate action.转载地址:http://ewbli.baihongyu.com/