问题描述
我创建了一个UIMenuController,并设置了一个自定义菜单项,如下所示:
I have created a UIMenuController and have set it a custom menu item like so:
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"Do This" action:@selector(item1)];
[menuController setMenuItems:[NSArray arrayWithObject:item1]];
但是我想让这个对象成为唯一出现的对象,所以我添加了这个代码:
But I wanted that object to be the only one to appear so I added this code:
- (BOOL)canPerformAction: (SEL)action withSender: (id)sender {
BOOL answer = NO;
if (action == @selector(item1))
answer = YES;
return answer;
}
问题是它仍然显示其他##标题##项目,例如选择,全选和粘贴。
这可能与显示在 UITextView
中有关。
但是如果显示所有其他项目,如何停止?
The problem is it still shows other## Heading ## items, such as "Select", "Select All" and "Paste".This may have something to do with this being displayed in a UITextView
.But how do I stop if from displaying all other items?
推荐答案
其中你想要子类 UITextView
。我只是试试这个与下面的代码,显示的唯一的菜单项是我 项目。
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.
从我的 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
$ c> MyTextView.h :
Code for my MyTextView.h
:
// MyTextView.h
#import <UIKit/UIKit.h>
@interface MyTextView :UITextView {
}
@end
的代码MyTextView.m
:
// MyTextView.m
#import "MyTextView.h"
@implementation MyTextView
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
}
@end
这篇关于UIMenuController自定义项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!