2013-07-19 1 views
12

Sto effettuando un UIAlertView con un input di testo.Disattivazione del pulsante UIAlertView

UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Save" message:@"Please Enter the Name of PDF" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput] 

quello che voglio fare quando UITextField è vuoto a disattivare il pulsante OK con una funzione di delegato

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
{ 

return NO; 
} 

Quando l'utente inizia a scrivere qualcosa nel campo di testo il pulsante OK dovrebbe diventare abilitato.

+0

Quindi, una volta la schermata 'UIAlertView' non contiene alcun pulsante per annullarla e chiuderla? – Desdenova

risposta

34

Si prega di provare questo

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView 
    { 
     /* Retrieve a text field at an index - 
      raises NSRangeException when textFieldIndex is out-of-bounds. 

     The field at index 0 will be the first text field 
     (the single field or the login field), 

     The field at index 1 will be the password field. */ 

     /* 
     1> Get the Text Field in alertview 

     2> Get the text of that Text Field 

     3> Verify that text length 

     4> return YES or NO Based on the length 
     */ 

     return [alertView textFieldAtIndex:0].text.length > 0; 

    } 
+2

In realtà, è sufficiente tornare; Il ternario è ridondante. return ([[[alertView textFieldAtIndex: 0] text] length]> 0) – Joe

10

Si dovrebbe fare un uso migliore di quella UIAlertViewDelegate metodo:

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

Si prega di notare che questo è un nuovo metodo delegato che è stato introdotto in iOS 5.0