Pregunta

Quiero mostrar un UIPickerView en convertirse en FirstResponder de un UITextField no llenar el teclado y el valor en el campo de texto formar el selector de vista.

cualquiera lo sabe?

¿Fue útil?

Solución

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

Es posible prevenir el teclado de aparecer. Establecer la clase de ser el delegado de la UITextField: textField.delegate = self y añadir: <UITextFieldDelegate> después de su declaración de interfaz (antes de la {) en el archivo de cabecera.

Ahora aplicar 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 a continuación es crear subclase de UITextField y anular los selectores *trackingWithTouch:event: y hacer su magia en ellos. Es probable que todavía necesita devolver NO a partir de textFieldShouldBeginEditting: para evitar que el teclado no se muestre.

Otros consejos

Utilice [campo de texto setInputView: pickerView];

Cambio de las vistas de entrada Sistema
inputView
  inputAccessoryView

Hola He pegado en algún 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/

añadir a inputview textField.

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

MyTextField.inputView = picker;

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

UITextView y UIPickerView con su propio UIToolbar

Esto hará que el pickerview a renunciar etc. Todo u necesidad de hacer luego es utilizar el método pickerview delegado 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 manera el teclado no se mostrará al tocar en ella. anula el evento toques en la UILabel, cuando eso ocurre llamar al método de formar la cuestión privious que mostrará la nueva vista.

 -(void) addToViewWithAnimation:(UIView *) theView
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top