[UIMenuController sharedMenuController] .isMenuVisible在EGOTextView中始终为NO

时间:2012-11-08 07:00:05

标签: iphone ios uimenucontroller

我使用EGOTextView进行NSAttributedString编辑。 UIMenuController上有一个我无法克服的错误。作为以下代码中描述的逻辑,在点击EGOTextView后,UIMenu将交替显示和隐藏。但事实是[UIMenuController sharedMenuController].isMenuVisible始终会 NO ,代码[menuController setMenuVisible:NO animated:NO];永远不会被调用。我错过了一些棘手的事吗?谢谢你的任何建议!

EGOTextView.m

- (void)tap:(UITapGestureRecognizer*)gesture {

NSLog(@"[UIMenuController sharedMenuController].isMenuVisible :%d",[UIMenuController sharedMenuController].isMenuVisible);
// It always log 0 even if the UIMenu is visible on the screen.

if (_editable && ![self isFirstResponder]) {
    [self becomeFirstResponder];  
    return;
}

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showMenu) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showCorrectionMenu) object:nil];

self.correctionRange = NSMakeRange(NSNotFound, 0);
if (self.selectedRange.length>0) {
    self.selectedRange = NSMakeRange(_selectedRange.location, 0);
}

NSInteger index = [self closestWhiteSpaceIndexToPoint:[gesture locationInView:self]];

if (_delegateRespondsToDidSelectURL && !_editing) {
    if ([self selectedLinkAtIndex:index]) {
        return;
    }
}

UIMenuController *menuController = [UIMenuController sharedMenuController];
NSLog(@"menuController.menuVisible :%d", menuController.menuVisible);

if ([menuController isMenuVisible]) {
// never run into here
    [menuController setMenuVisible:NO animated:NO];

} else {

    if (index==self.selectedRange.location) {
        [self performSelector:@selector(showMenu) withObject:nil afterDelay:0.35f];
    } else {
        if (_editing) {
            [self performSelector:@selector(showCorrectionMenu) withObject:nil afterDelay:0.35f];
        }
    }

}

[self.inputDelegate selectionWillChange:self];

self.markedRange = NSMakeRange(NSNotFound, 0);
self.selectedRange = NSMakeRange(index, 0);

[self.inputDelegate selectionDidChange:self];

}

2 个答案:

答案 0 :(得分:2)

我找到了下面的代码, [menuController setMenuVisible:NO animated:NO]; 应该是设置UIMenuController不可见的点。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    if ([gestureRecognizer isKindOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")]) {
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        if ([menuController isMenuVisible]) {
            [menuController setMenuVisible:NO animated:NO];
        }
    }

    return NO;

}

功能 - (void)点击:(UITapGestureRecognizer *)手势在 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 。

答案 1 :(得分:0)

也许你应该尝试搜索“setMenuVisible:NO”。它也必须写在其他地方。