Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling UIAlertView button

I am making a UIAlertView with a text input.

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

what i want to do when UITextField is empty i disable the OK button with a delegate function

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

return NO;
}

When the user begins to write something in the textfield the OK button should become enabled.

like image 552
hazem Avatar asked Jul 19 '13 13:07

hazem


2 Answers

Please try this

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

    }
like image 110
Vijay-Apple-Dev.blogspot.com Avatar answered Nov 18 '22 00:11

Vijay-Apple-Dev.blogspot.com


You should make better use of that UIAlertViewDelegate method:

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

Please note this is a new delegate method which was introduced in iOS 5.0

like image 10
Daniel Avatar answered Nov 17 '22 22:11

Daniel