2009-05-25 7 views

Respuesta

25

Editar: esta respuesta era correcta en el momento de la escritura. Apple ha introducido desde inputView. Mafonya answer es lo que deberías estar haciendo hoy en día.

Es posible evitar que aparezca el teclado. Configure su clase para que sea el delegado de UITextField: textField.delegate = self y agregue: <UITextFieldDelegate> después de su declaración de interfaz (antes del {) en el archivo de encabezado.

Ahora implemento textFieldShouldBeginEditing::

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    // Show UIPickerView 

    return NO; 
} 

Si usted quiere ser capaz de ocultar su pickerView este método no funcionará. Lo que podría hacer entonces es crear la subclase de UITextField y anular los selectores *trackingWithTouch:event: y hacer su magia en esos. Probablemente aún necesite devolver NO desde textFieldShouldBeginEditting: para evitar que se muestre el teclado.

+1

su solución funciona parcialmente. Deseo mostrar la vista del selector en algunos uitextfields específicos que no se encuentran en todos los campos de texto y también cuando el valor se selecciona desde la vista del selector que se encuentra en el campo de texto deseado, debe ocultarse la vista del selector. –

1

Salida del código al (hay tanto TextView + barra de pestañas y pickerview + código de barra de pestañas)

UITextView and UIPickerView with its own UIToolbar

esto hará que la pickerview a renunciar etc. Todo u necesidad de hacer luego es utilizar el pickerview delegar método para actualizar la vista de texto

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

en lugar de utilizar un campo de texto, utilice un UILabel, con un fondo blanco. De esa forma, el teclado no se mostrará cuando lo toques. anula el evento de toques en el UILabel, cuando eso sucede, llame al método de la pregunta privada que mostrará la nueva vista.

-(void) addToViewWithAnimation:(UIView *) theView 
+0

No quiero el teclado para mostrar sólo el selector de vista es que quiero que se mostrará cuando entra en el campo de texto –

+0

sí el teclado casi siempre se mostrará. deberás usar otra vista como uiLabel. He editado la respuesta para ser más conciso. – Bluephlame

+0

Como mencionó Blue, el teclado siempre aparecerá y no podrá evitarlo.Otra alternativa sería agregar una vista del selector en la parte superior del teclado. Tienen la misma altura, por lo que no se verá el teclado. – lostInTransit

4

Oye, he pegado un código que escribí hace un tiempo para cubrir este escenario. Hay dos ejemplos, uno con un actionsheet y uno sin un actionsheet:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{ 

      [myTextField resignFirstResponder]; 

      actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

      [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 

      CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 

      UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame]; 

       pickerView.showsSelectionIndicator = YES; 

       pickerView.dataSource = self; 

       pickerView.delegate = self; 

       [actionSheet addSubview:pickerView]; 

       [pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement 

       UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]]; 

       closeButton.momentary = YES; 

       closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f); 

       closeButton.segmentedControlStyle = UISegmentedControlStyleBar; 

       closeButton.tintColor = [UIColor blackColor]; 

       [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged]; 

       [actionSheet addSubview:closeButton]; 

       [closeButton release]; 

       [actionSheet showInView:displayedInView]; 

       [actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; 

} 

Este es el código sin el actionsheet:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{ 

     [myTextField resignFirstResponder]; 

      for (int component = 0; component &lt; (((NSInteger)numberOfComponentsForPickerView) - 1); component++) { 

       NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue]; 

       [pickerView selectRow:valueForRow inComponent:component animated:YES]; 

      } 

      [self fadeInPickerView]; 

      [view addSubview:pickerView]; 

      [pickerView release]; 

} 

Un ejemplo más detallado de esto se puede encontrar en: http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/

28

Utilice [textfield setInputView: pickerView];

Sustitución de la entrada del sistema Vistas
inputView
inputAccessoryView

+7

Esta es la respuesta correcta para aquellos que saben lo que están haciendo. –

+0

Acepto, esta es la respuesta correcta. –

+1

¿Ustedes saben la implementación rápida de esto? – kareem

4

complemento inputview a textField.

picker = [[UIPickerView alloc]init]; 
[picker setDataSource:self]; 
[picker setDelegate:self]; 
[picker setShowsSelectionIndicator:YES]; 

MyTextField.inputView = picker; 
+0

gracias! funciona para mí, pero ¿cómo ocultar el selector cuando tenemos que elegir? Muchas gracias –

+0

Funcionó para mí. Gracias –

Cuestiones relacionadas