UIMenuController 不显示

问题描述

我试图解决一个问题两天,但我真的不知道该怎么做。我尝试了几种解决方案,但没有任何帮助。

情况是这样:我有一个 UIViewController 并且我添加一个 UITextView 的子类作为子视图。

-(void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _textView = [[DPTextView alloc] initWithFrame:self.view.bounds];
    [_textView setText:[Nsstring stringWithContentsOfFile:_path encoding:NSUTF8StringEncoding error:nil]];
    _textView.editable = NO;
    _textView.selectable = YES;
    _textView.delegate = self;
    _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:_textView];
    //[self.view becomeFirstResponder];
    //[self becomeFirstResponder];
    //[_textView becomeFirstResponder];
}

当我打开这个 UIViewController 时,我看到了文字。如果我选择文本,则看不到 UIMenuController。我已经覆盖了 UITextView (DPTextView) 子类的一些方法,如下所示:

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

但它没有出现。一段时间后,我注意到如果我按照以下步骤操作,UIMenuController 会出现:

  1. 这个控制器来自另一个控制器。让我们称它们为 Controller1(具有 UISearchController 和 UITableView)和 Controller2(具有 DPTextView)。
  2. 我点击搜索栏进入 Controller1。
  3. 我点击搜索栏旁边的“取消”关闭键盘
  4. 我点击呈现 Controller2 的 UITableViewCell。

按照这些步骤,当我在 Controller2 中选择文本时会出现 UIMenuController!我真的不知道为什么!我认为按下搜索栏时发生了一些事情,比如控制器成为第一响应者或类似的东西(我已经尝试调用“becomeFirstResponder”,正如你在上面的代码中所读到的——注释的代码行)。

请注意,如果我在 Controller1 中点击并按住 UITableViewCell,我也应该获得 UIMenuController,因为我已经实现了这些方法

-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return TRUE;
}
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return (action == @selector(copy:));
}
-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    if (action == @selector(copy:)) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [[UIPasteboard generalPasteboard] setString:cell.textLabel.text];
    }
}

但它也没有出现在这里。使用搜索控制器后出现,所以同样的情况。

有什么建议吗?我不知道这是否有帮助,但 Controller1 和 Controller2 以模态呈现。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)