UIMenuController 不显示的问题

自定义了一个Label,想在长按label时弹出自定义的操作菜单,用到了UIMenuController。

思路很简单,在label中添加长按手势,实现-(BOOL)canBecomeFirstResponder和- (BOOL)canPerformAction:(SEL)action withSender:(id)sender方法:

-(BOOL)canBecomeFirstResponder

{

    return YES;

}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

    NSLog(@"%@",NSStringFromSelector(action));

    if(action == @selector(cut:) || action == @selector(copy:) || action == @selector(myCut:)|| action == @selector(myPaste:)){

        return YES;

    }

    else

    {

        return NO;

    }

}

但是menu一直没有显示出来。

后来发现我在项目的其它地方设置了某个window  makeKeyAndVisible,所以在-(BOOL)canBecomeFirstResponder中要额外做一些事情才能显示menu:

-(BOOL)canBecomeFirstResponder

{

    UIWindow *window = [[UIApplication sharedApplication].delegate window];

    if ([window isKeyWindow] == NO)

    {

        [window becomeKeyWindow];

        [window makeKeyAndVisible];

    }

    return YES;

}

你可能感兴趣的:(UIMenuController 不显示的问题)