2012-03-08 24 views
29

La actualización UIAlertView ahora tiene un estilo que permite a un campo de entrada de texto en UIAlertView es decirComo puedo init un campo de texto en un UIAlertView

alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

Esto funciona bien pero quería inicializar el texto de entrada con un defualt texto como "muestra".

veo que la gente en el pasado han utilizado la API no documentada similares (que funciona muy bien)

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"]; 

pero dado que esto todavía no es una API pública oficial de Apple no puede utilizarlo.

¿Alguna otra forma de manejar esto? Intenté iniciar en willPresentAlertView pero el campo de texto parece ser de solo lectura.

Gracias

Respuesta

8

No se ha probado, pero supongo que esto funcionaría:

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...]; 
UITextField* textField = [alert textFieldAtIndex:0]; 
textField.text = @"sample"; 
[alert show]; 
57

El UIALertView tiene un método textFieldAtIndex: que devuelve el objeto UITextField desea.

Para una UIAlertViewStylePlainTextInput, el índice del campo de texto es 0.

A continuación, puede establecer el marcador de posición (o texto) propiedad del campo de texto:

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
textField.placeholder = @"your text"; 

UIAlertView Class Reference

+0

Gran - usted que funcionó – timeview

+1

Usted es bienvenido :) Si tienes dudas, se puede aceptar la respuesta haciendo clic en el siguiente garrapata gracias al – Mutix

+0

Esto parece no funcionar más. textFieldAtIndex genera este error: *** Aplicación de finalización debido a excepción no detectada 'NSInvalidArgumentException', razón: 'textFieldIndex (0) está fuera de los límites de la matriz de campos de texto'. – Symmetric

6

Para el ajuste por defecto valor en uiAlertView esto funcionará.

UIAlertView *alert = .... 
UITextField *textField = [alert textFieldAtIndex:0]; 
[textField setText:@"My default text"]; 
[alert show]; 
0
- (IBAction)showMessage:(id)sender { 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add New Category" 
                 message:nil 
                delegate:self 
              cancelButtonTitle:@"Add" 
              otherButtonTitles:@"Cancel", nil]; 
    [message setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
    [message show]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Add"]) 
    { 
     UITextField *username = [alertView textFieldAtIndex:0]; 
     NSLog(@"Category Name: %@", username.text); 
    } 
} 
9

manera fácil de hacer

UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"your title" message:@"your message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    alerView.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [[alerView textFieldAtIndex:0] setPlaceholder:@"placeholder text..."]; 
    [alerView show]; 
+0

Respuesta perfecta :) –

Cuestiones relacionadas