2013-07-19 14 views
12

Wprowadzam UIAlertView z wprowadzeniem tekstowym.Wyłączanie przycisku UIAlertView

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

co chcę zrobić, gdy UITextField jest pusta wyłączyć przycisk OK z funkcji delegata

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

return NO; 
} 

Gdy użytkownik zaczyna pisać coś w polu tekstowym przycisk OK powinny zostać włączone.

+0

Więc raz Wyskakuje "UIAlertView", nie ma przycisku do anulowania i odrzucenia? – Desdenova

Odpowiedz

34

Spróbuj tego

- (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

Po prostu możesz wrócić; Trójskładnik jest zbędny. return ([[[alertView textFieldAtIndex: 0] tekst] length]> 0) – Joe

10

powinien lepiej wykorzystać tego UIAlertViewDelegate metody:

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

proszę zauważyć, że jest to nowa metoda delegata, który został wprowadzony w iOS 5.0