序列化与反序列化(NSDictionary),监听键盘

@interface ViewController ()

#define FileName @"Student.plist"

#define KName @"KName"
#define KBirth @"KBirth"
#define KSex @"KSex"
#define KNumber @"KNumber"
#define KHome @"KHome"

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow) name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillHide) name:UIKeyboardWillHideNotification object:nil];

    NSDictionary *dictionary=[NSDictionary dictionaryWithContentsOfFile:[self filePath]];
    if (dictionary) {
        self.nameField.text=[dictionary objectForKey:KName];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)save:(id)sender {

    do {
        NSString *name=[self.nameField text];
        NSString *bith=[self.birthField text];
        NSInteger sex=[self.sexSeg selectedSegmentIndex];
        NSString *home=[self.homeField text];
        NSString *number=[self.numberField text];
        if (!name||!bith||!home||!number) {
            break;
        }

        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:name forKey:KName];
        [dic setObject:bith forKey:KBirth];
        [dic setObject:[NSNumber numberWithInteger:sex] forKey:KSex];
        [dic setObject:number forKey:KNumber];
        [dic setObject:home forKey:KHome];

        if ([dic writeToFile:[self filePath] atomically:YES]) {
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"保存成功!" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
        return;

    } while (0);

    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"警告" message:@"基本信息不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

//返回沙盒路径
- (NSString *)filePath
{
    NSArray *array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=  [[array objectAtIndex:0] stringByAppendingPathComponent:FileName];
    return path;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nameField resignFirstResponder];
    [self.birthField resignFirstResponder];
    [self.numberField resignFirstResponder];
    [self.homeField resignFirstResponder];
}
- (void)KeyboardWillShow
{
    [UIView beginAnimations:@"show" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    CGRect rect=self.view.frame;
    rect.origin.y=-40;
    self.view.frame=rect;
    [UIView commitAnimations];
}
- (void)KeyboardWillHide
{
    [UIView beginAnimations:@"hide" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    CGRect rect=self.view.frame;
    rect.origin.y=0;
    self.view.frame=rect;
    [UIView commitAnimations];
}

时间: 2024-12-06 23:02:29

序列化与反序列化(NSDictionary),监听键盘的相关文章

java 事件监听 - 键盘

//事件监听 //键盘事件监听,写了一个小案例,按上下左右,改变圆形的位置,圆形可以移动 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Index extends JFrame{ //设置面板 Wdmb wdmb = new Wdmb(); public static void main(String[] args) throws Exception{ //实例化当前类 Index i

android监听键盘

android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为android:windowSoftInputMode="adjustResize"的时候,键盘的弹出和隐藏会触发onSizeChaged的事件,用下面的自定义RealativeLayout则能监听这个函数并判断键盘是弹出还是收起. /* * @Project: GZJK * @Autho

监听键盘

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; #pragma mark 监听方法 - (void)keyboardChangeFrame :(NSNotification *)note { CGFloat time = [note.userI

监听键盘frame变化

/** * 监听键盘的弹出和隐藏 */ - (void)keyboardWillChangeFrame:(NSNotification *)note { // 键盘最终的frame CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 动画时间 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey

iOS 键盘的监听 调整view的位置

iOS在处理键盘的出现和消失时需要监听UIKeyboardWillChangeFrameNotifications/UIKeyboardDidHideNotifications - (void)viewDidLoad { [super viewDidLoad]; //添加监听 键盘消失 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDown) name:UIKeyboardDid

js键盘监听操作

//js监听键盘事件 document.onkeydown = function (event) { var e = event || window.event || arguments.callee.caller.arguments[0]; alert(e.keyCode);//打印对应键盘的键值 if (e && e.keyCode == 13)//监听键盘Enter建 { } if (e && e.keyCode == 8)//监听键盘Backspace建 { } /

ios ---键盘的监听事件

//在view将要出现的时候重载viewWillAppear方法添加通知 监听事件 keyboardWillShow:  keyboardWillHide: - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:

iOS键盘监听以及获取键盘高度

在文本输入时,界面会弹出键盘.有时,当文本输入框过低,被键盘遮挡,使用户无法看见输入框文本内容,这就使得用户体验过低. 所以需要我们对键盘进行监控并获取键盘高度,调节界面或文本框高度进行处理.如下图,文本输入框过低的情况: 像这样的情况,如若未作处理,输入框就会被键盘遮挡.这时,我们需要监听键盘事件,获取键盘高度,对文本框视图进行高度调整: 1 #import "ViewController.h" 2 3 @interface ViewController ()<UITextFi

键盘监听,高度获取

监听 -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboarWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCente