2009-05-07 7 views

Respuesta

9

Sí, no está documentado. Para agregar un campo de texto a UIAlertView, use addTextFieldWithValue: label: method. Llama con el texto predeterminado como primer argumento y el texto que se muestra en un campo vacío como el segundo. Una vez hecho esto, puede acceder al campo mediante textFieldAtIndex: n - ver a continuación.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"  
         message:@"Give your full name" 
         delegate:self cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"OK", nil]; 
[alert addTextFieldWithValue:@""label:@"Name"]; 

// Customise name field 
UITextField* name = [alert textFieldAtIndex:0]; 
name.clearButtonMode = UITextFieldViewModeWhileEditing; 
name.keyboardType = UIKeyboardTypeAlphabet; 
name.keyboardAppearance = UIKeyboardAppearanceAlert; 
[alert show]; 

El siguiente fragmento de código muestra cómo recuperar el valor en el campo Nombre:

NSLog("Name is %@", [[modalView textFieldAtIndex:0] text]); 
+5

Como otras personas ya se ha mencionado, esta voluntad que te pateen desde la tienda de aplicaciones hoy en día. – zoul

+0

De acuerdo, esto fue escrito hace mucho tiempo. ¿Debo eliminar la respuesta? –

+0

@JaneSales: No eliminaría la respuesta, pero definitivamente agregaría un aviso importante y notable en la parte superior de su publicación. – FreeAsInBeer

3

Jeff Lamarche, reportaron algunas sample code on his blog para hacer precisamente esto. El formato parecía un poco inestable cuando lo probé, pero probablemente sea un buen punto de partida.

16

Aquí hay una forma de "Apple Approved" de hacerlo desde Tharindu Madushana. Me lo dio a su comentario en esta página: http://iosdevelopertips.com/undocumented/alert-with-textfields.html

// Ask for Username and password. 
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Twitter Details!" message:@"\n \n \n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

// Adds a username Field 
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
utextfield.placeholder = @"Username"; 
[utextfield setBackgroundColor:[UIColor whiteColor]]; 
[alertview addSubview:utextfield]; 

// Adds a password Field 
UITextField *ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 80.0, 260.0, 25.0)]; 
ptextfield.placeholder = @"Password"; 
[ptextfield setSecureTextEntry:YES]; 

[ptextfield setBackgroundColor:[UIColor whiteColor]]; [alertview addSubview:ptextfield]; 
// Move a little to show up the keyboard 
CGAffineTransform transform = CGAffineTransformMakeTranslation(0.0, 80.0); 
[alertview setTransform:transform]; 

// Show alert on screen. 
[alertview show]; 
[alertview release]; 

//... 
// Don't forget to release these after getting their values 
[utextfield release]; 
[ptextfield release]; 

Y por último para obtener el texto de nuevo

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) 
     return; //Cancel 

    UITextField *field = (UITextField *)[[alertView subviews] lastObject]; 
    NSLog (@"%@", field.text); 
} 
+0

Personalmente eliminaría la transformación y agregaría [utextfield becomeFirstResponder] después de liberar la vista de alerta para que el teclado aparezca automáticamente. – AlBeebe

5

Éste es realmente un viejas preguntas con respuestas muy viejos.

Esta es una muestra de cómo consigo un UITextField en un UIAlertView desde iOS 5:

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"New List Name" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Continue", nil]; 

    message.alertViewStyle = UIAlertViewStylePlainTextInput; 
    self.alertTextField = [message textFieldAtIndex:0]; 
    self.alertTextField.keyboardType = UIKeyboardTypeAlphabet; 
    message.delegate = self; 
    [message show]; 
    [self.alertTextField becomeFirstResponder]; 

donde alertTextField se estableció de esta manera:

@property (nonatomic, strong) UITextField *alertTextField; 
Cuestiones relacionadas