我希望的行为是:
当我单击我的自定义 UIMenuItem 时,它会显示另一个可供选择的 UIMenuItems 列表。我已经以这种方式实施了。
@implementation CustomUIWebView{
BOOL clickedShowSubMenu;
NSArray *mainMenuItems;
NSArray *subMenuItems;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
UIMenuController *mc = [UIMenuController sharedMenuController];
UIMenuItem *testSubMenu = [[UIMenuItem alloc] initWithTitle:@"TestSubMenu" action:@selector(testSubMenu:)];
mainMenuItems = [NSArray arrayWithObjects:testSubMenu, nil];
UIMenuItem *subItem1 = [[UIMenuItem alloc] initWithTitle:@"Item1" action:@selector(item1:)];
UIMenuItem *subItem2 = [[UIMenuItem alloc] initWithTitle:@"Item2" action:@selector(item2:)];
subMenuItems = [NSArray arrayWithObjects:subItem1, subItem2, nil];
[mc setMenuItems:mainMenuItems];
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMenuItemDismissed:) name:UIMenuControllerDidHideMenuNotification object:nil];
clickedShowSubMenu = NO;
}
return self;
}
- (void)testSubMenu:(id)sender{
UIMenuController *mc = [UIMenuController sharedMenuController];
[mc setMenuItems:subMenuItems];
mc.menuVisible = YES; //from te doc, it say it would keep the menu not dismiss when this function end
clickedShowSubMenu = YES;
}
- (void)item1:(id)sender{
}
- (void)item2:(id)sender{
}
- (void)onMenuItemDismissed:(id)sender{
if(clickedShowSubMenu == YES){
UIMenuController *mc = [UIMenuController sharedMenuController];
[mc setMenuItems:mainMenuItems];
clickedShowSubMenu = NO;
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if(action == @selector(testSubMenu:))return YES;
else if(action == @selector(item1:))return YES;
else if(action == @selector(item2:))return YES;
return NO;
}
@end
当我选择长按某些文本并单击 时Test Sub Menu
,它会起作用,它确实出现了“subMenu”:Item1
并且出现Item2
在相同的位置Test Sub Menu
。
但是,问题是第二次及以后。我选择了其他文本并且Test Sub Menu
正确显示,但是当我单击它时,它Item1
显示Item2
在它第一次出现的旧位置上。他们的立场从未改变。我不知道这是什么原因造成的。
编辑
好的。通过使用如何获取 UIWebView 中选定文本的坐标(CGRect)?获取选择的矩形边界并[mc setTargetRect:theRect inView:self];
在之前说mc.menuVisible = YES;
它工作并且子菜单显示与主菜单大致相同的位置。推荐吗?仍然有一点问题,当我突出显示整个段落时,主菜单显示在段落下方,但是,子菜单显示在段落上方。