One Quickie

Adding a pop up menu thingie (General->General)
On iOS, the menu you get when you long-press in a text field is called a UIMenuController, and you can pop one up for your own classes.

First, you need to be able to become first responder, and be able to perform the actions of the menu items you want displayed. The menu controller sifts through its default set, and actions your provide and sees if any can be performed. If not, they get dropped on the floor. This assumes that the menu being shown only has "Duplicate".

- (BOOL) canPerformAction: (SEL) action  withSender: (id) sender {
    return action == @selector(duplicate:);
} // canPerform

- (BOOL) canBecomeFirstResponder {
    return YES;
} // canBecomeFirstResponer
Then acquire an array of UIMenuItems (title / selector pairs), become first responder (that's important), and then figure out where you want the menu item to point to. Then show it:
    // Permanent set of items.  You might want to do something saner
   static NSArray *s_menuItems;

    if (s_menuItems == nil) {
        UIMenuItem *item = [[UIMenuItem alloc] initWithTitle: @"Duplicate"
                                               action: @selector(duplicate:)];
        s_menuItems = [ @[ item ] retain];
        [item release];

    [self becomeFirstResponder];  // important!

    // point the menu point somewhere
    CGPoint point = CGPointMake (160.0, 5.0);
    CGRect rect;
    rect.origin = point;
    rect.size = CGSizeMake (1.0, 1.0);

    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems = s_menuItems;

    [menu setTargetRect: rect  inView: self];
    [menu setMenuVisible: YES  animated: YES];
If you have work to do when the menu disappears (such as deselecting a tableview cell), you can listen for UIMenuControllerWillHideMenuNotification or one of its kin.

borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book