恢复textField数据

时间:2012-11-28 06:25:46

标签: iphone objective-c ios5

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

我使用的是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]; 
}

1 个答案:

答案 0 :(得分: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];

    }


}