2011-01-28 17 views
9

¿Cómo podemos implementar UIKeyboardTypeNumberPad para que tenga un botón 'hecho'? Por defecto no tiene uno.UIKeyboardTypeNumberPad sin un botón hecho

+0

¿Qué quiere decir mediante la aplicación de teclado, que desea ocultar el teclado o cualquier cosa ELSS. – Ishu

+0

No ... Solo que no habrá un botón hecho en el teclado – Abhinav

+1

Estimado pad numérico que no tiene ningún botón Hecho. De modo que su pregunta no es válida. – Ishu

Respuesta

13

Si no estoy equivocado, entonces desea preguntar cómo agregar un botón personalizado "Hecho" al teclado para UIKeyboardTypeNumberPad. En ese caso, esto podría ser útil. Declarar una in.h UIButton * DoneButton y agregue el código siguiente a .m archivo

- (void)addButtonToKeyboard { 
    // create custom button 
    if (doneButton == nil) { 
     doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)]; 
    } 
    else { 
     [doneButton setHidden:NO]; 
    } 

    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    // locate keyboard view 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard = nil; 
    for(int i=0; i<[tempWindow.subviews count]; i++) { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 
     // keyboard found, add the button 
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
       [keyboard addSubview:doneButton]; 
     } else { 
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
    } 
} 

- (void)doneButtonClicked:(id)Sender { 
//Write your code whatever you want to do on done button tap 
//Removing keyboard or something else 
} 

estoy usando la misma en mi aplicación y el marco del botón se ajusta por lo tanto, este modo puede llamar [auto addButtonToKeyboard] cada vez que se necesita mostrar el botón hecho sobre el teclado. UIKeyboardTypeNumberPad no tiene el botón Hecho de lo contrario. enter image description here

+0

Tengo un problema con la línea UIWindow * tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex: 1]; Obtengo una matriz emty sin subvistas – user784625

+0

Donde exactamente obtienes una matriz en blanco. En caso de que haya un teclado presente en la pantalla, obtendrá por lo menos un elemento en la matriz, es decir, el teclado. –

+0

He agregado la llamada a esta función una vez en keyboardwilllshow y keyboarddidshow pero sigo recibiendo el arreglo – user784625

1

iOS 5 problema resuelto. Un millón de gracias.

Tuve un problema al mostrar el botón Hecho.

sustituida:

if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
    [keyboard addSubview:doneButton]; 

que no muestra el botón hecho en iOS 5 ...

Con:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
    if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
     [keyboard addSubview:doneButton]; 
} else { 
    if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
     [keyboard addSubview:doneButton]; 
} 

funcionaba un lujo. Eres una estrella. Gracias, Nicola ;-)

1

Si alguien tuvo un problema con el botón DONE que sigue apareciendo cuando intenta cargar otros tipos de teclados en la misma aplicación, sé que muchas aplicaciones tienen este problema. De todos modos, así es como resolví esto: En su ViewController (el mismo controlador de vista que ha agregado el botón DONE) agregue este código (tal como está) y debería resolver sus problemas con el botón DONE continuar reapareciendo. Espero que ayude a algunas personas.

- (void)viewWillDisappear:(BOOL)animated { 
[super viewWillDisappear:animated]; 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil]; 

} else { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

}

2
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; 


//Call this method 

- (void)keyboardWillShow:(NSNotification *)note { 


    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)]; 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal]; 
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 
    for(int i=0; i<[tempWindow.subviews count]; i++) { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 

     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
     else { 
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
    } 

} 
Cuestiones relacionadas