禁用UIAlertView按钮

haz*_*zem 12 cocoa-touch objective-c ios

我正在UIAlertView使用文本输入.

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

当UITextField为空时我想做什么我禁用带有委托功能的OK按钮

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

return NO;
}
Run Code Online (Sandbox Code Playgroud)

当用户开始在文本字段中写入内容时,应该启用"确定"按钮.

Vij*_*com 34

请试试这个

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

    }
Run Code Online (Sandbox Code Playgroud)

  • 你实际上可以回来; 三元是多余的.return([[[alertView textFieldAtIndex:0] text] length]> 0) (2认同)

Dan*_*iel 10

您应该更好地使用该UIAlertViewDelegate方法:

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

请注意,这是iOS 5.0中引入的新委托方法