IOS第13天(1,私人通讯录,登陆功能,界面的跳转传值,自定义cell,编辑界面)
******HMLoginViewController 登陆的界面
#import "HMLoginViewController.h" #import "MBProgressHUD+MJ.h" #import "HMContactsTableViewController.h" @interface HMLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *accountField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UISwitch *autoLoginS;
@property (weak, nonatomic) IBOutlet UISwitch *rmbPwdS; @property (nonatomic, strong) HMContactsTableViewController *contact;
@end
@implementation HMLoginViewController
#warning 第四步做跳转之前的准备工作
/**
* 执行segue的时候,跳转之前调用
* 一般用做一些跳转之前的准备操作,给下一个控制器传值
* @param segue <#segue description#>
* @param sender <#sender description#>
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ // NSLog(@"%@---%@--%@",segue.identifier,segue.sourceViewController,segue.destinationViewController); // 获取目的控制器
UIViewController *v = segue.destinationViewController; // 给联系人导航条设置标题
v.navigationItem.title = [NSString stringWithFormat:@"%@的联系人",_accountField.text]; }
#warning 第三步处理登录功能
// 当点击登录的时候调用
- (IBAction)login:(id)sender { // hm 123 // 判断用户输入的账号和密码是否正确
if ([_accountField.text isEqualToString:@"hm"] && [_pwdField.text isEqualToString:@""]) { // 账号和密码正确 // 显示遮盖:只要做一些比较耗时的操作最好用遮盖
[MBProgressHUD showMessage:@"正在登录中"]; // GCD
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 移除遮盖
[MBProgressHUD hideHUD];
// 执行segue
[self performSegueWithIdentifier:@"login2contact" sender:nil];
}); }else{ // 不正确 // MBProgressHud:提示框
[MBProgressHUD showError:@"账号或者密码错误"];
}
}
#warning 第二步 处理Switch
// 当记住密码状态改变的时候调用
- (IBAction)rmbPwdSwitch:(UISwitch *)sender {
//
if (sender.isOn == NO) {
[_autoLoginS setOn:NO animated:YES]; } }
// 当自动登录状态改变的时候调用
- (IBAction)autoLoginSwitch:(UISwitch *)sender {
if (sender.isOn == YES) {
[_rmbPwdS setOn:YES animated:YES];
} }
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. #warning 第一步监听两个文本框的内容,控制器登录按钮的状态
// 1.addTarget
[_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;
}
@end
*************HMContactsTableViewController.m联系人的界面
#import "HMContactsTableViewController.h"
#import "HMAddViewController.h" #import "HMEditViewController.h" #import "HMContactCell.h" #import "HMContact.h"
@interface HMContactsTableViewController ()<UIActionSheetDelegate,HMAddViewControllerDelgegate,HMEditViewControllerDelegate> @property (nonatomic, strong) NSMutableArray *contacts;
@end
@implementation HMContactsTableViewController
#warning 第五步操作
#pragma mark - 注销功能
// 点击注销就会调用这个方法
- (IBAction)logout:(id)sender { UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"是否注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:nil, nil]; [sheet showInView:self.view]; }
- (NSMutableArray *)contacts
{
if (_contacts == nil) {
_contacts = [NSMutableArray array];
}
return _contacts;
} - (void)setName:(NSString *)name phone:(NSString *)phone
{
NSLog(@"%@---%@",name,phone);
}
/**
* 跳转之前,调用这个方法
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"%@",segue.destinationViewController); if ([segue.destinationViewController isKindOfClass:[HMAddViewController class]]) { HMAddViewController *vc = segue.destinationViewController; vc.delegate = self;
}else{ // 跳转到编辑控制器
HMEditViewController *edit = segue.destinationViewController; NSIndexPath *seletedIndex = [self.tableView indexPathForSelectedRow]; edit.delegate = self;
edit.contact = self.contacts[seletedIndex.row]; } } // 成功更新了一个联系人的时候调用
- (void)editViewController:(HMEditViewController *)edit didUpdateContact:(HMContact *)contact
{
// 刷新表格
[self.tableView reloadData];
} // 成功添加了一个联系人的时候调用
- (void)addViewController:(HMAddViewController *)add didAddContact:(HMContact *)contact
{
// 保存
[self.contacts addObject:contact]; // 刷新表格
[self.tableView reloadData];
} - (void)viewDidLoad
{
[super viewDidLoad]; // 移除分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.contacts.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ // 1.创建对象
HMContactCell *cell = [HMContactCell cellWithTableView:tableView]; // 2.传递模型
cell.contact = self.contacts[indexPath.row]; return cell;
} - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex) return; // 回到登录界面
[self.navigationController popViewControllerAnimated:YES];
} @end
************联系人的cell的界面 HMContactCell.m
#import "HMContactCell.h"
#import "HMContact.h" @interface HMContactCell() @property (nonatomic, weak) UIView *divide; @end @implementation HMContactCell - (UIView *)divide
{
if (_divide == nil) { UIView *v = [[UIView alloc] init]; v.backgroundColor = [UIColor blackColor]; v.alpha = 0.2;
_divide = v; [self.contentView addSubview:_divide];
} return _divide;
} - (void)setContact:(HMContact *)contact
{
_contact = contact; // 给cell的控件赋值
self.textLabel.text = contact.name;
self.detailTextLabel.text = contact.phone; } + (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"contact";
return [tableView dequeueReusableCellWithIdentifier:ID]; } // 从XIB加载完成的时候调用
- (void)awakeFromNib
{
// Initialization code }
// 当自己的frame改变的时候就会调用
- (void)layoutSubviews
{
[super layoutSubviews];
// 给divide设置位置
CGFloat divideH = ;
CGFloat divideW = self.bounds.size.width;
CGFloat divideY = self.bounds.size.height - divideH;
self.divide.frame = CGRectMake(, divideY, divideW, divideH); } - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end
************联系人的cell的界面 HMContactCell.h
#import <UIKit/UIKit.h>
@class HMContact;
@interface HMContactCell : UITableViewCell @property (nonatomic, strong) HMContact *contact; + (instancetype)cellWithTableView:(UITableView *)tableView; @end
************编辑的界面 HMEditViewController.m
#import "HMEditViewController.h" #import "HMContact.h" @interface HMEditViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn; @end @implementation HMEditViewController // 点击了保存按钮
- (IBAction)save:(id)sender {
// 1.回到联系人界面
[self.navigationController popViewControllerAnimated:YES];
// 2.更新数据:覆盖旧数据,把最新的数据展示出来
self.contact.name = _nameField.text;
self.contact.phone = _phoneField.text; // 3.通知联系人界面刷新表格
if ([_delegate respondsToSelector:@selector(editViewController:didUpdateContact:)]) {
[_delegate editViewController:self didUpdateContact:self.contact];
} } // 点击了编辑按钮就会调用
- (IBAction)edit:(UIBarButtonItem *)sender { if ([sender.title isEqualToString:@"取消"]) { // 点击取消
// 1.改变按钮的文字
sender.title = @"编辑";
// 2.不允许文本框编辑
_nameField.enabled = NO;
_phoneField.enabled = NO; // 3.隐藏保存按钮
_saveBtn.hidden = YES; // 4.退出键盘
// [_phoneField resignFirstResponder];
[self.view endEditing:YES]; // 恢复文本框显示
_nameField.text = _contact.name;
_phoneField.text = _contact.phone; }else // 点击编辑
{
// 1.改变按钮的文字
sender.title = @"取消";
// 2.允许文本框编辑
_nameField.enabled = YES;
_phoneField.enabled = YES; // 3.显示保存按钮
_saveBtn.hidden = NO; // 4.弹出电话的键盘
[_phoneField becomeFirstResponder]; } } #pragma mark 给文本框控件赋值
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. // 给控件赋值
_nameField.text = _contact.name;
_phoneField.text = _contact.phone; // 1.addTarget
[_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; } @end
************编辑的界面 HMEditViewController.h
#import <UIKit/UIKit.h> @class HMContact,HMEditViewController; @protocol HMEditViewControllerDelegate <NSObject> @optional
- (void)editViewController:(HMEditViewController *)edit didUpdateContact:(HMContact *)contact; @end @interface HMEditViewController : UIViewController @property (nonatomic, strong) HMContact *contact; @property (nonatomic, weak) id<HMEditViewControllerDelegate> delegate; @end
*****联系人的model HMContact.m
#import "HMContact.h" @implementation HMContact // instancetype自动识别当前类的对象 == HMContact *
+ (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone
{
HMContact *contact = [[HMContact alloc] init]; contact.name = name;
contact.phone = phone; return contact;
} @end
******HMContact.h
#import <Foundation/Foundation.h> @interface HMContact : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *phone; + (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone; @end
IOS第13天(1,私人通讯录,登陆功能,界面的跳转传值,自定义cell,编辑界面)的更多相关文章
- IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )
*****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...
- IOS第13天(2,私人通讯录,plist存储,偏好设置,归档)
***************plist存储 // 当点点击保存的时候调用 //保存 - (IBAction)save:(id)sender { // 获取沙盒的根路径 // NSString *ho ...
- iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面
iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面 在1.2.2小节中提到过编辑界面(Interface builder),编辑界面是用来设计用户界面的,单击打开Main. ...
- iOS 9应用开发教程之编辑界面与编写代码
iOS 9应用开发教程之编辑界面与编写代码 编辑界面 在1.2.2小节中提到过编辑界面(Interface builder),编辑界面是用来设计用户界面的,单击打开Main.storyboard文件就 ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- IOS学习之-私人通讯录
通过一段时间IOS的学习完成了一个简单的应用,"私人通讯录". 运行效果如下图: 1.登录页 2.通讯录列表 3.添加 4.编辑 5.删除 6.注销 总视图结构如下图: 总结本程序 ...
- 搭建私人通讯录/日历同步服务_使用cardDAV/calDAV服务
搭建私人通讯录/日历同步服务_使用cardDAV/calDAV服务 转载注明来源: 本文链接 来自osnosn的博客,写于 2020-02-18. Radicale, Radicale (对cardd ...
- .NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍
Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET Framework更注重与机器 ...
- 从零开始编写自己的C#框架(15)——Web层后端登陆功能
对于一个后端管理系统,最重要内容之一的就是登陆页了,无论是安全验证.用户在线记录.相关日志记录.单用户或多用户使用帐号控制等,都是在这个页面进行处理的. 1.在解决方案中创建一个Web项目,并将它设置 ...
随机推荐
- chrome插件
自备FQ神器,或者在公司浏览谷歌商店.话说我们公司电脑可以打开谷歌商店. 1.Performance-Analyser(网页性能分析) 这款插件是用来分析你的网页加载性能的,包括http请求,执行期的 ...
- css3 -- 网页字体
1.@font-face规则 @font-face{ font-family:chunk; src:local('chunkFive'), url("chunkFive.ttf“) form ...
- MySQL 挺有意思
1, 修改密码 mysql -u root -p update user set Password = PASSWORD('NEWPWD') WHERE user = 'root'; FLUSH PR ...
- Python基础9- 字典
#coding=utf8#字典由键和对应的值组成(键值对)--哈希表,字典元素也可以为空 dict1 = {'name':'kaly','age':20,'sex':'male'}dict2 = {} ...
- BZOJ4623 : Styx
$g$是积性函数,可以通过分解质因数在$O(n\log n \log\log n)$的时间内求出. 对于$((A\times B)\times C)\times D$,可以转化为$D\times (C ...
- Hadoop执行作业时报错:java.lang.OutOfMemoryError: Java heap space
常常被一些用户问到,说“为什么我的mapreduce作业总是运行到某个阶段就报出如下错误,然后失败呢?以前同一个作业没出现过的呀?” 10/01/10 12:48:01 INFO mapred.Job ...
- spring3.0注解
一.前言 在日常的开发过程中,我们基本上都是采用注解的方式进行开发,提升开发的效率.不管是struts2.spring.hibernate.或者ibatis,这样方便开发,减少配置文件的数量:有益于团 ...
- UVa 11922 & splay的合并与分裂
题意: 1个1—n的排列,实现一下操作:将a—b翻转并移动至序列的最后. SOL: splay维护区间的裸题——不过平衡树的题目貌似都是裸的吧...就是看操作的复杂程度罢... 如何取区间呢,我们在s ...
- 【HDU】3506 Monkey Party
http://acm.hdu.edu.cn/showproblem.php?pid=3506 题意:环形石子合并取最小值= =(n<=1000) #include <cstdio> ...
- Android -- 简单的图片浏览器
1. 效果图