保存文本字段数据但验证数据

时间:2014-12-24 10:13:10

标签: ios objective-c xcode

我有一个小应用程序,有2个按钮。 1按钮保存文本字段输入,另一个,将保存的输入加载到文本字段。 但我不知道如何验证。即使textfield为空,现在也在保存。 所以我想做以下验证:

如果textfield = null      然后不要保存。

My .m 

- (IBAction)save:(id)sender {

    // Frist Save String

    NSString *saveString = field1.text;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveString forKey:@"saveString"];
    [defaults synchronize];

    // Second Save String

    NSString *saveString2 = field2.text;
    NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
    [defaults2 setObject:saveString2 forKey:@"saveString2"];
    [defaults2 synchronize];


}
- (IBAction)load:(id)sender
{

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * loadString = [defaults objectForKey: @"saveString"];
    [field1 setText:loadString];

    NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
    NSString * loadString2 = [defaults2 objectForKey: @"saveString2"];
    [field2 setText:loadString2];

}


- (IBAction)dismiss1:(id)sender {
    [sender resignFirstResponder];

}


- (IBAction)dismiss2:(id)sender {
    [sender resignFirstResponder];




My .h

@interface ViewController : UIViewController
{
    IBOutlet UITextField *field1;
    IBOutlet UITextField *field2;


}

//Database Actions

- (IBAction)save:(id)sender;
- (IBAction)load:(id)sender;

//Keyboard Dismisses

- (IBAction)dismiss1:(id)sender;
- (IBAction)dismiss2:(id)sender;

3 个答案:

答案 0 :(得分:0)

- (IBAction)save:(id)sender
{
    if ([self ControlValidation]==YES)
    {
        NSString *saveString = field1.text;
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:saveString forKey:@"saveString"];
        [defaults synchronize];

        // Second Save String

        NSString *saveString2 = field2.text;
        NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
        [defaults2 setObject:saveString2 forKey:@"saveString2"];
        [defaults2 synchronize];
    }
}
-(BOOL)ControlValidation
{
    if ([self isEmpty:field1.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Text Field1 is    empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];
        return NO;
    }
    if ([self isEmpty:field2.text ]==YES)

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Text Field2 is empty" delegate: nil  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert  show];

        return NO;
    }
    return YES;
}


-(BOOL)isEmpty:(NSString *)str

{
    if (str == nil || str == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",str] length] == 0 || [[[NSString stringWithFormat:@"%@",str] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
    {
        return YES;

    }

    return NO;
}

答案 1 :(得分:0)

如果要保存任一字段,请执行以下操作,即使另一个字段为空。

- (IBAction)save:(id)sender {

// First Save String
  if([self textFieldTextIsValid:field1]){

    NSString *saveString = field1.text;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveString forKey:@"saveString"];
    [defaults synchronize];

  }


// Second Save String
  if([self textFieldTextIsValid:field2]){

    NSString *saveString2 = field2.text;
    NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
    [defaults2 setObject:saveString2 forKey:@"saveString2"];
    [defaults2 synchronize];

  }
}

-(BOOL)textFieldTextIsValid:(UITextField *)textfield{
  if([textfield.text length] !=0){
    return YES;
  }else{
    return NO;
  }
}

如果您只想在两个字段都有文字的情况下保存两个字段,请按以下步骤更改IBOutlet保存方法

- (IBAction)save:(id)sender {

  if([self textFieldTextIsValid:field1] && [self textFieldTextIsValid:field2]){

// First Save String
    NSString *saveString = field1.text;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveString forKey:@"saveString"];
    [defaults synchronize];


// Second Save String
    NSString *saveString2 = field2.text;
    NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
    [defaults2 setObject:saveString2 forKey:@"saveString2"];
    [defaults2 synchronize];

  }
}

这样,如果您添加其他条件来检查文本字段中输入文本的完整性,那么您只需将逻辑添加到“textFieldTextIsValid”方法并在类中的任何位置调用它。

答案 2 :(得分:-1)

试试这个

 - (IBAction)save:(id)sender {




        if([field1.text isEqualToString:@""] && [field2.text isEqualToString:@""]) {
          // There's no text in the textbox.
        }
        else{
           // Frist Save String

            NSString *saveString = field1.text;
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:saveString forKey:@"saveString"];
            [defaults synchronize];

            // Second Save String

            NSString *saveString2 = field2.text;
            NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
            [defaults2 setObject:saveString2 forKey:@"saveString2"];
            [defaults2 synchronize];

        }
        }