1

我的视图中有几个文本字段,我在其中接受用户的输入。该视图包含必填和可选字段。我已经对必填字段进行了验证。每当我检查每个必填字段的验证。如果必填字段的验证失败,则会弹出 UIAlertView。每次 AlertView 弹出剩余文本文件中的所有信息时都会被清除。如何确保文本字段上的数据不会被擦除。

我正在使用的警报视图

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Validation"
                                                        message:@"Please enter name."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release]; 

编辑 1 我试过 delegate:self了,但还是一样

编辑 2

if(userName.text.length>0 )
{
// Process further
}
else{
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Validation"
                                                            message:@"Please enter name."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release]; 
}
4

1 回答 1

0

UITextFieldDelegate在您的文件中设置.h并声明UITextField *txtfldFirstName;

 txtfldFirstName=[[UITextField alloc]initWithFrame:CGRectMake(110, 110, 160, 25)];
    txtfldFirstName.backgroundColor=[UIColor clearColor];
    txtfldFirstName.text=@"";
    txtfldFirstName.autocapitalizationType=NO;
    txtfldFirstName.borderStyle=UITextBorderStyleLine;
    txtfldFirstName.delegate=self;
    [self.view addSubview:txtfldFirstName];

在您执行的操作中

if ([txtfldFirstName.text isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Validation"
                                                            message:@"Please enter name."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

}
else
{
// your operation if textfield not empty
}

否则使用

-(void)textFieldDidEndEditing:(UITextField *)textField
{

 if (textField==txtfldFirstName) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Validation"
                                                                message:@"Please enter name."
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
                [alert show];

    }


}
于 2012-11-28T07:16:58.323 回答