2013-07-19 9 views
12

UIAlertViewにテキスト入力をしています。私はUITextFieldのが空のときに何をしたいのかUIAlertViewボタンを無効にする

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

、ユーザがOKボタンが有効になる必要があるテキストフィールドに何かを書き始めると、私は、デリゲートの機能を

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

return NO; 
} 

を[OK]ボタンを無効にします。

+0

で導入された新しいデリゲートメソッドであることに注意してください'UIAlertView'がポップすると、キャンセルするボタンがありません。 – Desdenova

答えて

34

あなたはそのUIAlertViewDelegate方法をより有効に活用する必要があり、この

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

実際には返すことができます。三元は冗長です。 return([[alertView textFieldAtIndex:0] text] length]> 0) – Joe

10

を試してください:

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

これがそう一度のiOS 5.0

関連する問題