显示UIMenuController丢失键盘 [英] Showing UIMenuController loses keyboard

查看:1102
本文介绍了显示UIMenuController丢失键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个类似于手机上的留言应用程序的iphone应用程序。我只是设置了通过UIMenuController复制邮件的能力,但如果键盘显示,有人试图复制一个消息,键盘消失(可能是因为我的 [cell becomeFirstResponder]; 其中 cell 是正在复制的消息单元格)。

I'm making an iphone app similar to the Messages app that comes on the phone. I just set up the ability to copy messages via a UIMenuController, but if the keyboard is showing and someone tries to copy a message, the keyboard goes away (presumably because of my [cell becomeFirstResponder]; where cell is the message cell being copied).

有没有办法显示复制讯息而不会丢失键盘?

Is there a way to show the Copy message without losing the keyboard?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    //...other cell setup stuff...

    UILongPressGestureRecognizer *longPressGesture =
    [[UILongPressGestureRecognizer alloc]
      initWithTarget:self action:@selector(showCopyDialog:)];
    [cell addGestureRecognizer:longPressGesture];

    return cell;
}

- (void)showCopyDialog:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        ConvoMessageCell *cell = (ConvoMessageCell *)[gesture view];
        NSIndexPath *indexPath = [self.tblConvo indexPathForCell:cell];

        UIMenuController *theMenu = [UIMenuController sharedMenuController];
        [cell becomeFirstResponder];
        [theMenu setTargetRect:CGRectMake(menuX, menuY, 100, 100) inView:cell];
        [theMenu setMenuVisible:YES animated:YES];        
    }
}


推荐答案

I解决这个困境通过子类化UITextView提供一种方法来覆盖nextResponder和禁用内置的操作(粘贴),像这样:

I solved this dilemma by subclassing UITextView to provide a way to override the nextResponder and disable the built-in actions (Paste), like so:

@interface CustomResponderTextView : UITextView

@property (nonatomic, weak) UIResponder *overrideNextResponder;

@end







@implementation CustomResponderTextView

@synthesize overrideNextResponder;

- (UIResponder *)nextResponder {
    if (overrideNextResponder != nil)
        return overrideNextResponder;
    else
        return [super nextResponder];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (overrideNextResponder != nil)
        return NO;
    else
        return [super canPerformAction:action withSender:sender];
}

@end

处理程序,检查文本视图是否已经是第一个响应者。如果是,请覆盖下一个响应者;否则键盘可能隐藏了,你可以简单地 becomeFirstResponder 。您还必须在菜单隐藏时重置覆盖:

Then, in your gesture action handler, check whether the text view is already the first responder. If so, have it override the next responder; otherwise the keyboard is probably hidden anyway and you can simply becomeFirstResponder. You'll also have to reset the override when the menu hides:

if ([inputView isFirstResponder]) {
    inputView.overrideNextResponder = self;
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(menuDidHide:)
        name:UIMenuControllerDidHideMenuNotification object:nil];
} else {
    [self becomeFirstResponder];
}

- (void)menuDidHide:(NSNotification*)notification {

    inputView.overrideNextResponder = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self
        name:UIMenuControllerDidHideMenuNotification object:nil];
}

使用iOS 5中引入的表视图委托方法( shouldShowMenuForRowAtIndexPath 等)不是一个解决方案,我,我需要控制菜单的位置(默认情况下它只是水平居中在单元格,但我显示信息气泡和希望菜单以实际气泡为中心)。

Using the table view delegate methods introduced in iOS 5 (shouldShowMenuForRowAtIndexPath etc.) wasn't a solution for me as I needed control over the positioning of the menu (by default it's simply horizontally centered over the cell, but I'm displaying message bubbles and wanted the menu centered over the actual bubble).

这篇关于显示UIMenuController丢失键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆