如何在警报视图中单击UIButton时关闭UIAlertView

时间:2014-12-08 08:11:12

标签: ios objective-c

在UIAlertView中单击按钮时关闭UIAlertView的正确方法是什么。我想在点击按钮时不检查视图时检查UITextField中是否没有字符串。

  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      //Add alert delegate
    if (alertView.tag == kDeleteAlertTag)
    {

        if (buttonIndex == 0)
        {
            [self.emails removeObjectAtIndex:currentIndex];
            [self.plistReader saveToPlistName:kPlistEmails fromArray:self.emails];
            [self.tblEmails reloadData];
        }
    }

    // remove email alert delegate
    if (alertView.tag == kAddEmailTag)
    {
        if (buttonIndex == 0)
        {

            // get text Input Alert
            UITextField * emailInputTextField = [alertView textFieldAtIndex:0];

            // get string from text Alert
            NSString *textInput = [emailInputTextField text];

            if (self.emails== nil) {
                self.emails = [[NSMutableArray alloc]init];
            }
            if ([textInput isEqualToString:@""]) {
                NSLog(@"Not have string");
            }else
            if ([KUtils NSStringIsValidEmail:textInput] == YES)
            {
                [self.emails addObject:textInput];

                // save to plist and reload table
                [self.plistReader saveToPlistName:kPlistEmails fromArray:self.emails];
                [self.tblEmails reloadData];

            }else if([KUtils NSStringIsValidEmail:textInput] == NO)
            {

                // not dismiss alert in hre
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

不要针对文本字段验证字符串验证

 if (emailInputTextField.text && emailInputTextField.text.length > 0)
{
//do something 
}
else
{
//All fields are required UIAlert here
}

修改

您也可以尝试:

if ([emailInputTextField.text length] != 0) {
//Do something
}
else {
//All fields are required UIAlert here
}