数据的保存(自己弄个plist存储数据)

来源:互联网 发布:短文本相似度算法 编辑:程序博客网 时间:2024/06/01 03:58

// save方法用于保存用户填写的信息

- (IBAction)save:(UIButton*)sender {

    // 获取填入表格中的数据

    NSString *name=self.nameField.text;

    NSString *birthday=self.birthdayField.text;

    NSIntegersex=self.sexSegment.selectedSegmentIndex;

    NSString *number=self.numberField.text;

    // 判断表格填写是否完整

    if(!name.length||!birthday.length||!number.length){

        UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:

@"信息不完整,请重新填写" delegate:nil cancelButtonTitle:@"确定"

otherButtonTitles:nil];

        [alert show];

        return;

    }

     [self.view endEditing:YES];

    // 创建字典对象封装学生信息

    NSMutableDictionary*Student=[[NSMutableDictionary alloc]init];

    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];

    [Student setObject:nameforKey:@"Name"];

    [Student setObject:birthday forKey:@"Birthday"];

    [Student setObject:[NSNumbernumberWithInteger:sex] forKey:@"Sex"];

    [Student setObject:number forKey:@"Number"];

    [dic setObject:StudentforKey:@"Student"];

    // 将字典对象转为属性列表持久保存在plist文件中

    if ([dic writeToFile:[self filePath]atomically:YES]) {

   UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"保存成功"

                    delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];

        [alert show];

    }

    return;

}

// 获取plist文件的路径

- (NSString *) filePath

{

    // 获取应用程序的沙盒目录

    NSArray*array=NSSearchPathForDirectoriesInDomains

                              (NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *path=[array objectAtIndex:0];

    return [pathstringByAppendingPathComponent:FileName];

}

// 屏幕单击事件响应

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES]; // 退出键盘

}

// 按回车键,切换文本框的输入焦点

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if (textField == self.nameField) {

        [self.birthdayFieldbecomeFirstResponder];  // 切换到birthField

    }else if (textField == self.birthdayField){

        [self.numberFieldbecomeFirstResponder]; // 切换到numberField

    }

    return YES;

}

//依次为三个输入框添加代理


 (2)当用户填写信息时,由于屏幕有限,键盘弹起后会遮挡填写数据的页面,导致用户无法操作,针对这种情况,将整个程序界面上移是一个很好的解决方法。接下来,在viewDidLoad方法中

添加键盘弹起和关闭事件的监听,之后在事件响应方法中改变Y坐标,利用UIView的动画效果,实现应用程序界面弹起和落下的效果,具体代码如下所示:

- (void)viewDidLoad {

    [super viewDidLoad];

    // 注册通知监听器,监听键盘弹起事件

    [[NSNotificationCenter defaultCenter]addObserver:self

         selector:@selector(keyboardWillShow:)

              name:UIKeyboardWillShowNotificationobject:nil];

    // 注册通知监听器,监听键盘收起事件

    [[NSNotificationCenter defaultCenter]addObserver:self

         selector:@selector(keyboardWillHide:)

             name:UIKeyboardWillHideNotificationobject:nil];

}

// 键盘弹出时激发该方法

– (void)keyboardWillShow:(NSNotification*)notification

{

    // 开始视图升起动画效果

    [UIView beginAnimations:@”keyboardWillShow”context:nil];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    // 获取主视图View的位置

    CGRect rect = self.view.frame;

    rect.origin.y = -60;

    // 更改主视图View的位置

    self.view.frame = rect;

    // 结束动画

    [UIView commitAnimations];

}

// 键盘关闭时激发该方法

– (void)keyboardWillHide:(NSNotification*)notification

{

    // 开始视图下降动画效果

    [UIView beginAnimations:@”keyboardWillHide”context:nil];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    // 获取主视图View的位置

    CGRect rect = self.view.frame;

    rect.origin.y = 0;

    // 恢复主视图View的位置

    self.view.frame = rect;

    // 结束动画

    [UIView commitAnimations];

}

– (void)dealloc

{

    // 移除通知监听器

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}


0 0
原创粉丝点击