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

前端 未结 2 2004
滥情空心
滥情空心 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: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
    

提交回复
热议问题