2011-12-05 21 views
7

Estoy haciendo una aplicación para iPhone similar a la aplicación de mensajes que viene en el teléfono. Acabo de configurar la capacidad de copiar mensajes a través de un UIMenuController, pero si el teclado se muestra y alguien intenta copiar un mensaje, el teclado se va (presumiblemente debido a mi [cell becomeFirstResponder]; donde cell es la celda de mensajes que se está copiando).Mostrando UIMenuController pierde el teclado

¿Hay alguna manera de mostrar el mensaje Copiar sin perder el teclado?

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

Respuesta

7

En iOS 5, ahora se puede utilizar los métodos de delegado vista de tabla para mostrar el menú Regulador:

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

muestra el controlador de menú de esta manera no va a renunciar al teclado.

Todavía tengo curiosidad, ya que tengo una aplicación que admite pre-iOS 5 y me gustaría hacer lo que usted dice también (no renuncie al teclado cuando aparezca el menú de copia).

+0

Esto podría funcionar, pero el MenuController se colocará justo en el centro horizontal de la celda. –

16

He resuelto este dilema subclasificando UITextView para proporcionar una manera de anular el nextResponder y desactivar la incorporada en acciones (Pegar), así:

@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 

Luego, en su gestor de acciones con gestos, comprueba si la vista de texto ya es la primera respuesta. Si es así, anule al siguiente respondedor; de lo contrario, el teclado probablemente esté oculto de todos modos y simplemente puede becomeFirstResponder. Usted también tiene que restablecer la anulación cuando el menú se esconde:

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

mediante la vista de tabla de métodos de delegado introducidos en iOS 5 (shouldShowMenuForRowAtIndexPath etc.) no era una solución para mí, ya que necesitaba control sobre el posicionamiento del menú (por defecto está simplemente centrado horizontalmente sobre la celda, pero estoy mostrando burbujas de mensaje y quería que el menú se centrara sobre la burbuja real).

+0

¡Gran respuesta, gracias! cualquier bibliografía para esto? – AmitP

+0

Esto funciona en parte. El MenuController que aparece pertenece al CustomResponderTextView y no a la celda. Alguna idea para arreglar eso? –

Cuestiones relacionadas