12-UI进阶(私人通讯录和主流框架)

2016-03-21  本文已影响102人  木喳喳的夏天

TableView的点击处理

通过代码的方式搭建编辑界面

#pragma mark - tableView代理方法
// 点击cell的时候调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    // 加载storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    // 创建编辑控制器
     XMGEditViewController *editVc = [storyboard instantiateViewControllerWithIdentifier:@"edit"];
    
    // 给控制器传递模型数据
    editVc.contact = self.contacts[indexPath.row];
    
    // 跳转到编辑界面
    [self.navigationController pushViewController:editVc animated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置导航条的标题
    self.title = @"查看/编辑界面";
    
    // 设置导航条右边的按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(edit:)];
    
    // 给文本框
    _nameField.text = _contact.name;
    _phoneField.text = _contact.phone;

    // 给文本框添加监听器,及时监听文本框内容的改变
    [_nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [_phoneField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    
    // 判断下保存按钮能否点击
    [self textChange];
    
}

// 任一一个文本框的内容改变都会调用
- (void)textChange
{
    _saveBtn.enabled = _nameField.text.length && _phoneField.text.length;

}

保存功能

    // 还原数据
    _nameField.text = _contact.name;
    _phoneField.text = _contact.phone;

block讲解

// name为block的类型别名,params为block中需要的参数
// block的类型名应该以类名开头,后面加上Block
typedef void(^name)(params);

@property (nonatomic, strong) MyBlock myBlock1;
// inline
// blockName:block变量名
returnType(^blockName)(parameterTypes) = ^(parameters) {
    statements
};

通讯录中使用block进行逆传

  1. 编辑联系人后保存
// 定义block类型别名
typedef void(^XMGEditViewControllerBlock)();

@property (nonatomic, strong)XMGEditViewControllerBlock block;

iOS应用数据存储的常用方式

  1. XML属性列表(plist)归档

Plist存储

Preference偏好设置存储

// 保存数据
- (IBAction)save:(id)sender {
    // NSUserDefaults为单例,即程序运行中只分配一块内存
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:@"xmg" forKey:@"account"];
    [userDefaults setObject:@"123" forKey:@"pwd"];
    [userDefaults setBool:YES forKey:@"rmbPwd"];
    // 在iOS7之前,默认不会马上把跟硬盘同步
    // 同步
    [userDefaults synchronize]; 
}

// 读取数据
- (IBAction)read:(id)sender {
   NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwd"];
    
   NSLog(@"%@",pwd);
}

storyboard中快速复制控件的方法:command + d

自定义对象归档

- (IBAction)save:(id)sender {
    
    Person *p = [[Person alloc] init];
    p.age = 18;
    
    // 获取cache
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    // 获取文件的全路径
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
    
    // 把自定义对象归档
    [NSKeyedArchiver archiveRootObject:p toFile:filePath];
    
}
// 什么时候调用:自定义对象归档的时候

// 作用:用来描述当前对象里面的哪些属性需要归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    // name
    [aCoder encodeObject:_name forKey:@"name"];
    
    // age
    [aCoder encodeInt:_age forKey:@"age"];
    
}

自定义对象解档

- (IBAction)read:(id)sender {
    
    // 获取cache
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    // 获取文件的全路径
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
    
    // 解档
    Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    NSLog(@"%d",p.age);
    
}
// 什么时候调用:解档对象的时候调用

// 作用:用来描述当前对象里面的哪些属性需要解档
// initWithCoder:就是用来解析文件的
- (id)initWithCoder:(NSCoder *)aDecoder
{
    // super:NSObject
    #warning 什么时候需要调用initWithCoder
    if (self = [super init]) {
        // 注意:一定要给成员变量赋值
        // name
       _name = [aDecoder decodeObjectForKey:@"name"];
        
        // age
       _age = [aDecoder decodeIntForKey:@"age"];
    }
    return self;
}

什么时候调用initWithCoder和initWithFrame

主流框架的搭建

self.hidesBottomBarWhenPushed = YES;
上一篇 下一篇

猜你喜欢

热点阅读