Adding a dynamic custom UIMenuItem to Copy & Paste Menu before it shows

前端 未结 2 2000
滥情空心
滥情空心 2021-02-10 17:02

I have successfully been able to add a custom UIMenuItem to the Copy & Paste menu in my iPhone app, and even subclassed UITextView to get r

相关标签:
2条回答
  • 2021-02-10 17:23

    If the question is still relevant then you could use the UIMenuControllerWillShowMenuNotification or the UIMenuControllerDidShowMenuNotification notification.
    See the documentation here.

    Code sample:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowMenu:) name:UIMenuControllerWillShowMenuNotification object:nil];
    
    0 讨论(0)
  • 2021-02-10 17:47

    At startup somewhere:

    UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
    [UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObject:testMenuItem];
    [testMenuItem release];
    

    And in your UITextView or UITextField subclass:

    @implementation MyTextView
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if (action == @selector(test:)) {
            // Return YES only if it's possible to perform the action at this time
            return YES;
        }
        return [super canPerformAction:action withSender:sender];
    }
    - (void)test:(id)sender {
        // Perform the action here
    }
    @end
    
    0 讨论(0)
提交回复
热议问题