簡體   English   中英

顯示UIMenuController丟失鍵盤

[英]Showing UIMenuController loses keyboard

我正在創建一個類似於手機上的消息應用程序的iPhone應用程序。 我只是設置了通過UIMenuController復制消息的能力,但如果鍵盤顯示並且有人試圖復制消息,鍵盤就會消失(可能是因為我的[cell becomeFirstResponder];其中cell是要復制的消息單元) 。

有沒有辦法顯示復制郵件而不會丟失鍵盤?

- (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];        
    }
}

我通過繼承UITextView以提供一種覆蓋nextResponder並禁用內置操作(Paste)的方法來解決這個難題,如下所示:

@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 當菜單隱藏時,您還必須重置覆蓋:

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等)對我來說不是一個解決方案,因為我需要控制菜單的位置(默認情況下,它只是在單元格上水平居中,但我正在顯示消息氣泡並希望菜單以實際氣泡為中心)。

在iOS 5中,您現在可以使用表視圖委托方法來顯示菜單控制器:

- (BOOL) tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;

以這種方式顯示菜單控制器不會使鍵盤重新簽名。

我仍然對此感到好奇,因為我有一個支持iOS 5之前的應用程序,我也想做你所說的(當出現復制菜單時不要退出鍵盤)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM