03小码哥通讯录(数据存储)
// // XMGContact.m // 小码哥通讯录 #import "XMGContact.h"@implementation XMGContactstatic NSString *nameKey = @"name"; static NSString *phoneKey = @"phone";- (void)encodeWithCoder:(NSCoder *)aCoder {// name.phone [aCoder encodeObject:_name forKey:nameKey];[aCoder encodeObject:_phone forKey:phoneKey]; }- (id)initWithCoder:(NSCoder *)aDecoder {if (self = [super init]) {_name = [aDecoder decodeObjectForKey:nameKey];_phone = [aDecoder decodeObjectForKey:phoneKey];}return self; }+ (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone {XMGContact *c = [[self alloc] init];c.name = name;c.phone = phone;return c; } @end
// XMGContactViewController.m // 小码哥通讯录 #import "XMGContactViewController.h" #import "XMGAddViewController.h" #import "XMGEditViewController.h" #import "XMGContact.h"#define XMGFilePath [ NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"contacts.data"] @interface XMGContactViewController ()<UIActionSheetDelegate>@property (nonatomic, strong) NSMutableArray *contacts;@end@implementation XMGContactViewController- (NSMutableArray *)contacts {if (_contacts == nil) {// 读取数据 接档接档接档_contacts = [NSKeyedUnarchiver unarchiveObjectWithFile:XMGFilePath];// 判断下有没有读取数据if (_contacts == nil) {_contacts = [NSMutableArray array];}}return _contacts; }// 跳转之前的时候调用 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// 给添加控制器传递联系人控制器属性XMGAddViewController *addVc = segue.destinationViewController;addVc.block = ^(XMGContact *contact){// 1.联系人添加到数组 [self.contacts addObject:contact];// 2.刷新表格 [self.tableView reloadData];// 3.保存联系人,注意:如果归档数组,底层会遍历数组元素一个一个归档 [NSKeyedArchiver archiveRootObject:self.contacts toFile:XMGFilePath];};}// 点击注销的时候调用 - (IBAction)logout:(id)sender {// 弹出actionSheetUIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"是否注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:nil, nil];[sheet showInView:self.view];} #pragma mark - UIActionSheetDelegate // 点击UIActionSheet控件上的按钮调用 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {if (buttonIndex == 0) { // 点击了注销 [self.navigationController popViewControllerAnimated:YES];}}- (void)viewDidLoad {[super viewDidLoad];// // 取消分割线self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;// tableView有数据的时候才需要分割线// 开发小技巧:快速取消分割线self.tableView.tableFooterView = [[UIView alloc] init];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.contacts.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {// 创建标示符static NSString *ID = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];}// 获取模型XMGContact *c = self.contacts[indexPath.row];cell.textLabel.text = c.name;cell.detailTextLabel.text = c.phone;return cell; }#pragma mark - tableView代理方法 // 点击cell的时候调用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {// 加载storyboardUIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];// 创建编辑控制器XMGEditViewController *editVc = [storyboard instantiateViewControllerWithIdentifier:@"edit"];editVc.contact = self.contacts[indexPath.row];// block:点击编辑的时候调用这个blockeditVc.block = ^(){ // 刷新表格 [self.tableView reloadData];// 3.保存联系人,注意:如果归档数组,底层会遍历数组元素一个一个归档 [NSKeyedArchiver archiveRootObject:self.contacts toFile:XMGFilePath];};// 跳转到编辑界面 [self.navigationController pushViewController:editVc animated:YES];}// 只要实现这个方法,就会有滑动删除功能 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {// 删除模型 [self.contacts removeObjectAtIndex:indexPath.row];// 删除tableViewCell[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"增加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {// 点击增加的时候调用NSLog(@"增加");}];action.backgroundColor = [UIColor greenColor];UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {// 点击增加的时候调用NSLog(@"删除");}];return @[action,action1]; } @end
// // XMGLoginViewController.m // 小码哥通讯录 // // Created by xiaomage on 15/6/12. // Copyright (c) 2015年 xiaomage. All rights reserved. // #import "XMGLoginViewController.h"#import "MBProgressHUD+XMG.h"@interface XMGLoginViewController ()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UIButton *loginBtn;@property (weak, nonatomic) IBOutlet UITextField *accountField; @property (weak, nonatomic) IBOutlet UITextField *pwdField;@property (weak, nonatomic) IBOutlet UISwitch *rmbPwdSwitch; @property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch; @end /*来源控制器传递给目的控制器:顺传数据传值:1.接收方一定要有属性接收2.传递方必须要拿到接收方*/ /*1.[self performSegueWithIdentifier]2.创建segue3.设置来源控制器segue.sourceViewController = self4.创建目的控制器,segue.destinationViewController = 目的控制器5.[self prepareForSegue]跳转之前的准备操作6.[segue perform]7.判断下segue的类型,如果是push,拿到导航控制器push[self.navigationController pushViewController:segue.destinationViewController animated:YES];*/ @implementation XMGLoginViewController#define XMGUserDefaults [NSUserDefaults standardUserDefaults]static NSString *accountKey = @"account"; static NSString *pwdKey = @"pwd"; static NSString *rmbKey = @"rmd"; static NSString *loginKey = @"login";// 在执行跳转之前的时候调用 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {UIViewController *vc = segue.destinationViewController;vc.title = [NSString stringWithFormat:@"%@的联系人列表", _accountField.text];NSLog(@"%@--%@",segue.sourceViewController,segue.destinationViewController); }// 点击了登录按钮的时候调用 // xmg 123 - (IBAction)login:(id)sender {// 提示用户,正在登录ing...[MBProgressHUD showMessage:@"正在登录ing..."];dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// 隐藏蒙版 [MBProgressHUD hideHUD];// 验证下账号和密码是否正确if ([_accountField.text isEqualToString:@"xmg"] && [_pwdField.text isEqualToString:@"123"]) { // 输入正确// 数据存储// 账号,密码,记住密码,自动登录 [XMGUserDefaults setObject:_accountField.text forKey:accountKey];[XMGUserDefaults setObject:_pwdField.text forKey:pwdKey];[XMGUserDefaults setBool:_rmbPwdSwitch.on forKey:rmbKey];[XMGUserDefaults setBool:_autoLoginSwitch.on forKey:loginKey];// 直接跳转// 跳转到联系人界面[self performSegueWithIdentifier:@"login2Contact" sender:nil];}else{ // 账号或者密码错误// 提示用户账号或者密码错误[MBProgressHUD showError:@"账号或者密码错误"];}});}// 记住密码开关状态改变的时候调用 - (IBAction)rmbPwdChange:(id)sender {// 如果取消记住密码,自动登录也需要取消勾选if (_rmbPwdSwitch.on == NO) { // 取消记住密码// 取消自动登录 [_autoLoginSwitch setOn:NO animated:YES];}}// 自动登录开关状态改变的时候调用 - (IBAction)autoLoginChange:(id)sender {// 如果勾选了自动登录,记住密码也要勾选if (_autoLoginSwitch.on == YES) {[_rmbPwdSwitch setOn:YES animated:YES];}}- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.// 读取数据NSString *account = [XMGUserDefaults objectForKey:accountKey];NSString *pwd = [XMGUserDefaults objectForKey:pwdKey];BOOL rmb = [XMGUserDefaults boolForKey:rmbKey];BOOL login = [XMGUserDefaults boolForKey:loginKey];_accountField.text = account;if (rmb == YES) {_pwdField.text = pwd;}_rmbPwdSwitch.on = rmb;_autoLoginSwitch.on = login;// 勾选自动登录if (login == YES) {[self login:nil];}// 给文本框添加监听器,及时监听文本框内容的改变 [_accountField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];[_pwdField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];// 判断下登录按钮能否点击 [self textChange];}// 任一一个文本框的内容改变都会调用 - (void)textChange {_loginBtn.enabled = _accountField.text.length && _pwdField.text.length;NSLog(@"%@--%@",_accountField.text,_pwdField.text); }@end
转载于:https://www.cnblogs.com/laugh/p/6651386.html
03小码哥通讯录(数据存储)相关推荐
- 零基础轻松学python pdf 小码哥_零基础轻松学Python
零基础轻松学Python电子书 Python之父Guido van Rossum力荐的Python门书.本书以通俗易懂的语言.好玩有趣的案例让读者轻轻松松.循序渐地从零始掌握Python 3 编程.本 ...
- 小码哥iOS--玩转【斗鱼直播APP】系列之直播总体概览
直播总体概览 概述 直播的现状 2016年,是一个直播年.直播行业快速发展,同时也滋生了大大小小上千家相关的公司. 中国互联网络信息中心发布的报告显示,截至今年6月,我国网络直播用户规模达到3.25亿 ...
- MT小码哥的跳棋游戏+新篇
小码哥的跳棋游戏 小码哥喜爱跳棋.跳棋游戏在一条直线上,一共n 个位置(1~n ),每个位置有 2个状态: 0表示没有棋子,1表示有棋子.小码哥的棋子自然是能通过没有棋子的位置.当面前有 1个棋子时, ...
- 百度松果 小码哥考研小技巧 (概率期望)
题目描述 要考研了,小码哥非常焦急,他要开始背英语单词,每个单词的长度是lil_ili ,小码哥有pipipi的概率认识这个单词,小码哥要从一个有nnn个单词的总清单里按顺序的背单词,小码哥有一个认 ...
- 你好2017! 再见2015, 再见小码哥!
一根筋教育总监李南江致全体同仁的一封公开信 各位同仁,同学们: 大家好! 2015年的今天我发布了一篇日志"决定加入一个全新的IT培训品牌小码哥(中英双语版)", 怀揣着对教育的热 ...
- 小码哥考研技巧(求背单词长度的最大期望)
输入: 3 100 6 100 8 100 10 输出: 326.12 考点:1.求最大期望的话需要对n个单词的长度和认识概率进行动态数组排序 2.对精度有所要求 理论解析:无论小码哥有没有背到认识的 ...
- 小码哥iOS拓展班2期
小码哥iOS拓展班2期,视频全套不加密,有FM,直播,RAC,汇编项目,源码文档齐全. 注: 视频分为破解版和重录版,破解版码哥课堂和直播项目不全:重录版所有的视频和文档都是齐全的,只有直播项目第一天 ...
- 可怜的小码哥 马蹄集
可怜的小码哥 难度:黄金 时间限制:1秒 巴占用内存:128M 提瓦特大陆上有一个贫穷的占星术士小码哥,出于占星术的要求,他时常要解 决一些困难的数学问题.这天,他有遇到一个难题:对于一给定的素数集合 ...
- css动画唯美背景,小码哥-利用CSS3渐变实现唯美背景图
原标题:小码哥-利用CSS3渐变实现唯美背景图 CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果.但是,通过使用 CSS3 渐 ...
最新文章
- JAVA入门笔记·JAVA名词及环境搭建
- 问题小结(二)——maven的核心功能、面向对象编程和面向接口编程的区别、抽象类和接口的区别等
- PowerBI随笔(6)-filter、caculate、RELATED相关
- JQuery真的不难~第六回 JQ中的异步调用方式
- Dapr + .NET 实战(十-终篇)K8S运行Dapr
- docker 安全性_使用最新的安全性增强来调整Docker
- 阿里淘宝:“杀死”拼多多!
- python自动化办公真的好用吗-用 Python 自动化办公能做到哪些有趣或有用的事情?...
- 【转】JavaScript常用代码书写规范
- mysql清除内存不足_MySQL内存不足怎么办
- python操作数据库的两种方式
- CEOI2017 D1T3 mousetrap 树形dp+二分答案
- html label 背景图片,Qt添加窗口背景图片、Label图片显示、、Label文字显示
- Hadoop介绍和环境配置
- Verilog语言- 和c语言的区别
- PhpStorm在上传文件的时候提示 Upload to 虚拟机 failed: invalid descendent file name \.
- 考研:2023寒假复习及2022复试问题
- 名帖52 王献之 小楷《洛神赋》
- LTE----013 UE开机后的第一件事: PLMN选择
- 两化融合贯标之-申请表案例