UIMenuController Custom Items

房东的猫 提交于 2019-11-27 17:54:49
Stefan Arentz

I think this is one of the few cases where you want to subclass UITextView. I just tried this with the following code, and the only menu item that is shown is my Do Something item.

From my TestViewController.m

@implementation TestViewController

- (void) doSomething: (id) sender
{
    NSLog(@"Doing something");
}

- (void) viewDidLoad
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    UIMenuItem *item = [[[UIMenuItem alloc] initWithTitle: @"Do Something"
        action: @selector(doSomething:)] autorelease];
    [menuController setMenuItems: [NSArray arrayWithObject: item]];
}

@end

Code for my MyTextView.h:

//  MyTextView.h

#import <UIKit/UIKit.h>

@interface MyTextView :UITextView {

}

@end

Code for MyTextView.m:

//  MyTextView.m

#import "MyTextView.h"

@implementation MyTextView

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!