20

我正在尝试使以下代码工作:

UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setTargetRect: CGRectMake(100, 100, 100, 100) inView: self.view];
[menu setMenuVisible: YES animated: YES];

菜单实例已准备好但未显示 - 宽度始终为零。

或者有没有关于这个UIPasteboard/UIMenuController主题的示例代码?

4

9 回答 9

51

即使我阅读了您的所有答案,我也无法使其正常工作。我正在展示适用于所有人的现成代码。

假设我们有一个名为 Controller 的控制器类。您可以简单地将以下代码粘贴到此控制器,以使菜单在其视图上工作:


- (void)loadView {
    [super loadView];

    UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.view addGestureRecognizer:gr];    
}

- (void) longPress:(UILongPressGestureRecognizer *) gestureRecognizer {
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"Item" action:@selector(menuItemClicked:)];

        NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
        [menuController setMenuItems:[NSArray arrayWithObject:resetMenuItem]];
        [menuController setTargetRect:CGRectMake(location.x, location.y, 0.0f, 0.0f) inView:[gestureRecognizer view]];
        [menuController setMenuVisible:YES animated:YES];
    }
}

- (void) copy:(id) sender {
    // called when copy clicked in menu
}

- (void) menuItemClicked:(id) sender {
    // called when Item clicked in menu
}

- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender {
    if (selector == @selector(menuItemClicked:) || selector == @selector(copy:)) {
        return YES;
    }
    return NO;
}

- (BOOL) canBecomeFirstResponder {
    return YES;
}

为了使菜单工作,必须做的是 firstResponder(在我们的例子中,我们的控制器 - 参见 [self becomeFirstResponder] 行)必须能够成为第一响应者(覆盖方法 canBecomeFirstResponder 导致默认实现返回 NO)以及因为- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender这应该对可以执行的任何操作返回 YESfirstResponder

于 2010-10-06T13:01:37.337 回答
7

如果您正在实现自定义视图并且该视图应该是响应者(与其他视图相反,如 UITextField),则需要在视图中覆盖 canBecomeFirstResponder 函数并返回 YES:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

然后,当您显示菜单时,您应该执行以下操作:

- (void)myMenuFunc {
    if (![self becomeFirstResponder]) {
        NSLog(@"couldn't become first responder");
        return;
    }

    UIMenuController *theMenu = [UIMenuController sharedMenuController];
    CGRect selectionRect = CGRectMake(0, 0, 0, 0);
    [theMenu setTargetRect:selectionRect inView:self];
    [theMenu setMenuVisible:YES animated:YES];
}
于 2009-09-02T03:37:34.200 回答
7

万一有人仍然有问题:我的菜单曾经工作过,有一天奇迹般地停止工作了。我的应用程序中的其他所有内容仍然有效。现在我已经[window makeKeyAndVisible]从方法中删除了方法application:didFinishLaunchingWithOptions:,虽然其他一切仍然有效,但这打破了UIMenuController

我这边的愚蠢错误,很难找到罪魁祸首......

于 2011-09-21T19:20:55.903 回答
4

要显示 UIMenuController 必须添加以下内容

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(cut:))
        return NO;
    else if (action == @selector(copy:))
        return YES;
    else if (action == @selector(paste:))
        return NO;
    else if (action == @selector(select:) || action == @selector(selectAll:))
        return NO;
    else
        return [super canPerformAction:action withSender:sender];
}
于 2010-01-11T13:02:36.413 回答
3

我认为 Cam 是对的,需要同时覆盖 canPerformAction 和 canBecomeFirstResponder

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(doSomething:)) {
        return YES;
    }
    return NO;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}
于 2010-09-02T08:10:40.200 回答
1
// MyView.h

@interface MyView : UIView {
    IBOutlet UITextField * textField_;
}

@end

// MyView.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"show menu");

    [textField_ becomeFirstResponder];
    // [self.window becomeFirstResponder];

    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setTargetRect: CGRectMake(0, 0, 100, 10) inView: self];
    [menu setMenuVisible: YES animated: YES];

    NSLog(@"menu width %f, visible %d", menu.menuFrame.size.width, menu.menuVisible);
}

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

您需要在 canPerformAction:withSender: 中添加更多代码 - 它应该检查粘贴板和您的选择状态。Apple 的 iPhone 应用程序编程指南提供了几个代码片段。

于 2009-07-18T21:53:53.723 回答
0

我是通过以下方式完成的。只需在很短的初始化延迟后调用显示菜单的方法。我不想从视图控制器调用它,也没有找到表明我的自定义视图出现并且我准备好显示菜单的事件。所以从我的角度来看,这样就可以了。延迟可以减少,但这取决于你。

@implementation DTSignatureImageView

- (id)initWithImage:(UIImage *)image
{
    self = [super initWithImage:image];
    if(self){
        self.contentMode = UIViewContentModeScaleAspectFit;
        self.frame = CGRectMake(0, 0, image.size.width / 2.5, image.size.height / 2.5);
        self.userInteractionEnabled = YES;

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(signatureDidPan:)];
        [self addGestureRecognizer:pan];

        [self becomeFirstResponder];

        [self performSelector:@selector(showMenu) withObject:nil afterDelay:0.5];
    }

    return self;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)showMenu
{
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems = @[
       [[UIMenuItem alloc] initWithTitle:@"Apply" action:@selector(applySignature)],
       [[UIMenuItem alloc] initWithTitle:@"Update" action:@selector(updateSignature)],
       [[UIMenuItem alloc] initWithTitle:@"Clear" action:@selector(delegateSignature)]];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES animated:YES];
}

- (NSArray *)menuActions
{
    static NSArray *actions = nil;
    if (actions == nil){
        actions = @[
                    NSStringFromSelector(@selector(applySignature)),
                    NSStringFromSelector(@selector(updateSignature)),
                    NSStringFromSelector(@selector(delegateSignature))];
    }

    return actions;
}

- (void) signatureDidPan: (UIPanGestureRecognizer *)gesture
{
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan: {
            [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
            break;
        }

        case UIGestureRecognizerStateEnded: {
            [self becomeFirstResponder];
            [self showMenu];
        }

        default:
            break;
    }

    CGPoint point = [gesture locationInView:gesture.view.superview];
    gesture.view.center = point;
}
于 2014-06-03T13:50:16.853 回答
0

您不必将UIMenuController* menuEG 添加到主视图或子视图self.view吗?我认为这就像[self.view addSubView:menu.view];或者我错过了你的问题的重点。您可能还想设置菜单视图的框架。

于 2009-07-18T05:30:32.457 回答
0

UIMenuController 没有视图。我刚刚从苹果的iPhone 应用程序编程指南中搜索了一些代码:事件处理

清单 3-4 显示编辑菜单

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *theTouch = [touches anyObject];

    if ([theTouch tapCount] == 2  && [self becomeFirstResponder]) {

        // selection management code goes here...

        // bring up editing menu.
        UIMenuController *theMenu = [UIMenuController sharedMenuController];
        CGRect selectionRect = CGRectMake(currentSelection.x, currentSelection.y, SIDE, SIDE);
        [theMenu setTargetRect:selectionRect inView:self];
        [theMenu setMenuVisible:YES animated:YES];
    }
}
于 2009-07-18T07:17:00.807 回答