Kevin Sylvestre

A Ruby and iOS developer and designer living in Santa Monica, California.
A Ruby and iOS developer and designer living in Santa Monica, California.
  • About
  • Contact
  • rss
  • archive
  • Cocoa Copy Paste Menu

    I spent some time earlier today playing with the UIMenuController available in the iOS SDK and realized two thing: sometimes Apple’s examples suck and having an environment that often gives no feedback is terrible.

    In this case, I implemented a copy of an example that was supposed to present a semi-modal cut / copy / paste menu. It didn’t. After spending nearly an hour trying to identify why nothing was happening, I found I had missed copying a line. Anyways, for the record, here is the concise example of how to present a UIMenuController:

    //
    //  SampleViewController.m
    //  Sample
    //
    //  Created by Kevin Sylvestre on 10-03-25.
    //  Copyright Kevin Sylvestre 2010. All rights reserved.
    //
    
    #import "SampleViewController.h"
    
    @implementation SampleViewController
    
    #pragma mark - Helpers
    
    - (void)menu
    {
      [self becomeFirstResponder];
    
      UIMenuController *menu = [UIMenuController sharedMenuController];
    
      [menu setTargetRect:CGRectMake(100, 100, 0, 0) inView:self.view];
      [menu setMenuVisible:YES animated:YES];
    }
    
    #pragma mark - Main
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
      [self menu];
    }
    
    - (BOOL)canBecomeFirstResponder
    {
      return YES;
    }
    
    #pragma mark - Actions
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
      if (action == @selector(cut:) || action == @selector(copy:)) return YES;
      return NO;
    }
    
    - (void)cut:(id)sender { NSLog(@"cut"); }
    - (void)copy:(id)sender { NSLog(@"copy"); }
    
    @end
    
    • March 25, 2010 (2:16 am)
    • #cocoa
    • #iphone
    • #ipad
    • #apple
    • #mac
© 2009–2013 Kevin Sylvestre