我有UIWebView。我需要从UIWebView中选择一些文本。所以即时通讯使用UIMenuController for MenuItem。我需要在按钮点击时显示UIMenuController。但它不起作用。如果我将代码粘贴到ViewDidLoad中,它就可以正常工作。
代码:
[wbCont loadHTMLString:webString baseURL:nil];
[self.view addSubview:wbCont];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 120.0, 160.0, 40.0);
[button addTarget:self
action:@selector(show:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Shows" forState:UIControlStateNormal];
[self.view addSubview:button];
-(void)show:(id)sender{
NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
if (!items) items = [[NSMutableArray alloc] init];
UIMenuItem *menuItem;
menuItem = [[UIMenuItem alloc] initWithTitle:@"BookMark" action:@selector(book:)];
[items addObject:menuItem];
[menuItem release];
menuItem = [[UIMenuItem alloc] initWithTitle:@"Note" action:@selector(note:)];
[items addObject:menuItem];
[menuItem release];
[[UIMenuController sharedMenuController] setMenuItems:items];
[items release];
}
答案 0 :(得分:1)
可能有点太晚了,正如两年前被问到的那样,但如果有人 - 像我一样 - 遇到过:
您忘了让menuController可见。
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
在你的结尾 - (void)show:(id)sender;方法应该做到了。
欢呼;)