2011-06-07 7 views
7

Quiero que aparezca un UIPickerView cuando presiono un botón (como el teclado) y luego desaparezco cuando el usuario toca en cualquier parte de la pantalla. ¿Cómo puedo hacer esto? Gracias.¿Cómo puedo presentar una vista del selector como lo hace el teclado?

Un poco más de fondo: Tengo un UITextField llamado meses en UITableViewCell. Ahora, la mejor opción para mí es poner allí un UIPikcerView y será más fácil para el usuario simplemente elegir el mes en lugar de tener que escribirlo (y es la mejor manera). Pero UIPickerView se ve muy feo en una UITableViewCell, sin mencionar que no puedo cambiar su gigantesco tamaño. Entonces, ¿qué debería hacer en este caso?

+0

Su título no es muy descriptivo. Proporcione más detalles sobre lo que intenta lograr (en el título) – Julian

+0

Disculpe. El título fue actualizado por otra persona, pero ahora también proporcioné más antecedentes a esta pregunta. – sumderungHAY

Respuesta

13

Así es como debería usar UIPickerView para un campo de texto en una UITableView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle= UITableViewCellSelectionStyleNone; 
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    textField.clearsOnBeginEditing = NO; 
    textField.textAlignment = UITextAlignmentRight; 
    textField.delegate = self; 
    [cell.contentView addSubview:textField]; 
    //textField.returnKeyType = UIReturnKeyDone; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

     // if you want a UIPickerView for first row... then do the following // 
     // Here.. packSizePickerView is an object of UIPickerView 

    if (indexPath.row == 1) 
    { 
    textField.tag=0; 
    textField.delegate= self; 
    packSizePickerView.delegate = self; 
    packSizePickerView = [[UIPickerView alloc] init]; 
    packSizePickerView.autoresizingMask=UIViewAutoresizingFlexibleWidth; 
    packSizePickerView.showsSelectionIndicator=YES; 
    textField.inputView = packSizePickerView; 
    } 

    // if you want to select date/months in row 2, go for UIDatePickerView // 

    if (indexPath.row == 1) 
    { 
     textField.tag = 1; 
     UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 
     datePicker.datePickerMode = UIDatePickerModeDate; 
     [datePicker addTarget:self action:@selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged]; 
     datePicker.tag = indexPath.row; 
     textField.delegate= self; 
     textField.inputView = datePicker; 
     [datePicker release]; 

    } 

} 

// Configure the cell... 

NSInteger row = [indexPath row]; 
cell.textLabel.text = [myArray objectAtIndex:row]; 

return cell; 

}

Y para datePickerValueChangedd

- (void)datePickerValueChangedd:(UIDatePicker*) datePicker{ 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)]; 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
df.dateStyle = NSDateFormatterMediumStyle; 
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]; 

    NSLog(@"I am inside the datePickerValueChanged - - %@", label.text); 
[df release]; 
    globalValue1 = label.text; 

    // use a global variable to copy the value from the pickerView. // 
    }  

Ahora en el textFieldDidEndEditing:

-(void) textFieldDidEndEditing:(UITextField *)textField { 
if (textField.tag ==0) 
    [textField setText: globalValue0]; 

if (textField.tag ==1) 
    [textField setText: globalValue1]; 

} 

Y a renunciar UIDatePicker, establezca la UIView como uicontrol y

resignFirstResponder 
+0

¡¡Gran respuesta !! ¡Eso es exactamente lo que estaba buscando! +1 – Garoal

0

Creo que debe colocar su UIPickerView "fuera" de la pantalla visible y animarlo cuando se presione el botón. No estoy seguro de cuál sería la mejor manera de deshacerse de él, pero probablemente podría escuchar un toque en la vista principal cuando el selector es visible y luego animarlo de nuevo.

Cuestiones relacionadas