How to get UIMenuController work for a custom view?

前端 未结 9 1191
旧时难觅i
旧时难觅i 2020-12-02 10:56

I\'m trying to get the following code work:

UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setTargetRect: CGRectMake(100, 100, 100,         


        
相关标签:
9条回答
  • 2020-12-02 11:34

    In case somebody still has problems: My menu used to work and some day stopped working miraculously. Everything else in my app still worked. Now I had removed the [window makeKeyAndVisible] method from application:didFinishLaunchingWithOptions: method, and while everything else still worked, this breaks UIMenuController!

    Stupid error on my side, difficult to find the culprit...

    0 讨论(0)
  • 2020-12-02 11:34

    UIMenuController doesn't have a view. I just searched some code from apple's iPhone Application Programming Guide: Event Handling:

    Listing 3-4 Displaying the editing menu

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *theTouch = [touches anyObject];
    
        if ([theTouch tapCount] == 2  && [self becomeFirstResponder]) {
    
            // selection management code goes here...
    
            // bring up editing menu.
            UIMenuController *theMenu = [UIMenuController sharedMenuController];
            CGRect selectionRect = CGRectMake(currentSelection.x, currentSelection.y, SIDE, SIDE);
            [theMenu setTargetRect:selectionRect inView:self];
            [theMenu setMenuVisible:YES animated:YES];
        }
    }
    
    0 讨论(0)
  • to display UIMenuController one has to add following

    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        if (action == @selector(cut:))
            return NO;
        else if (action == @selector(copy:))
            return YES;
        else if (action == @selector(paste:))
            return NO;
        else if (action == @selector(select:) || action == @selector(selectAll:))
            return NO;
        else
            return [super canPerformAction:action withSender:sender];
    }
    
    0 讨论(0)
提交回复
热议问题