2011-02-24 30 views

Respuesta

1

Puede crear dos botones para Aceptar y Cancelar. Luego, agregue esos dos como vistas secundarias en el UIAlertView.Actuando el texto (longitud del texto) en el campo de texto, Puede realizar acciones de habilitar y deshabilitar.

0

Sin conocer el contexto de su aplicación, es posible que lo siguiente no se aplique, pero ¿ha leído el iOS Human Interface Guidelines? Parece que es mejor que encuentres una alternativa a UIAlertView si esto es algo que se mostrará al usuario con frecuencia.

0

No está realmente relacionado con su pregunta, pero no modifica el UIAlertView predeterminado si no desea que su aplicación sea rechazada. Si no estoy equivocado, estás agregando campos de texto a la vista de alerta, ¿no? Como una vista de inicio de sesión. Deberías crear tu propia vista.

Por lo tanto, con respecto a su pregunta, cree su vista, configure los botones se ha deshabilitado y delegue los UITextFields. Cuando se llama

- (void)textFieldDidBeginEditing:(UITextField *)textField; 

, habilite esos botones.

37

Sólo publicar esto para actualizar la respuesta desde iOS 5:

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 
    UITextField *textField = [alertView textFieldAtIndex:0]; 
    if ([textField.text length] == 0) 
    { 
    return NO; 
    } 
    return YES; 
} 

ACTUALIZACIÓN: iOS 8 Desde Apple han desaprobado la UIAlertView a favor de la UIAlertController. Ya no es una llamada delegado a alertViewShouldEnableFirstOtherButton:

Así que en lugar deberá ajustar los botones de propiedad enabled a través de la UITextFieldTextDidChangeNotification Añadir un Textview a la alerta con

  • (void) addTextFieldWithConfigurationHandler: (void (^) (* UITextField textField)) configurationHandler
[<#your alert#> addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
textField.delegate = self; 
textField.tag = 0; //set a tag to 0 though better to use a #define 
}]; 

luego implementar el método delegado

  • textFieldDidBeginEditing (void): (UITextField *) textField
- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
//in here we want to listen for the "UITextFieldTextDidChangeNotification" 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(textFieldHasText:) 
             name:UITextFieldTextDidChangeNotification 
             object:textField]; 

} 

Cuando el texto en textField cambia invocará una llamada a "textFieldHasText: "y transmitir una NSNotification *

-(void)textFieldHasText:(NSNotification*)notification{ 
//inside the notification is the object property which is the textField 
//we cast the object to a UITextField* 
if([[(UITextField*)notification.object text] length] == 0){ 
//The UIAlertController has actions which are its buttons. 
//You can get all the actions "buttons" from the `actions` array 
//we have just one so its at index 0 

[<#your alert#>.actions[0] setEnabled:NO]; 
} 
else{ 

[<#your alert#>.actions[0] setEnabled:YES]; 
} 
} 

No olvide quitar su observador cuando termine

+2

Ojalá pudiera votar más de una vez. Muchas gracias. –

4

Quería extender la respuesta de Ryan Forsyth agregando esto. Si agrega un UIAlertView con estilo predeterminado, puede obtener una excepción fuera de rango si intenta acceder a un campo de texto ya que no existe ninguno, por lo que primero debe verificar su estilo de vista.

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView 
{ 
    if(alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput || 
     alertView.alertViewStyle == UIAlertViewStylePlainTextInput || 
     alertView.alertViewStyle == UIAlertViewStyleSecureTextInput) 
    { 
     NSString* text = [[alertView textFieldAtIndex:0] text]; 
     return ([text length] > 0); 
    } 
    else if (alertView.alertViewStyle == UIAlertViewStyleDefault) 
     return true; 
    else 
     return false; 
} 
Cuestiones relacionadas