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项目,并将它设置 ...
随机推荐
- WPF点补间、拟合回归直线
1,path画刷,绘制正弦 点,线: 生成正弦点 profilePoint.Value = * ( - Math.Sin(i * Math.PI / )); profilePoint.Type = ; ...
- 页面内容排序插件jSort的使用
当页面列表内容很多的时候,我们可能需要将内容按照某个方式进行排序,比如按照字母或者大小等排序.本文将使用排序插件jSort来对页面内容进行排序. jSort插件可以对页面任何内容进行排序(ta ...
- wpf 触发器,属性触发器,事件触发器,事件触发器。
<EventTrigger RoutedEvent="Mouse.MouseEnter"/> <DataTrigger Binding="{Bindin ...
- Java 生成16/32位 MD5
http://blog.csdn.net/codeeer/article/details/30044831
- HDU4609 3-idiots(母函数 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...
- ubuntu下命令行禁用笔记本触摸板
机房电脑不好用,所以用笔记本,但是由于笔记本过分紧凑手经常让鼠标不知道跑哪里去.于是找到了这两个命令 禁用:sudo rmmod psmouse 启用:sudo modprobe psmouse 非常 ...
- 【BZOJ】1108: [POI2007]天然气管道Gaz
题意 \(n\)个黑点\(n\)个白点(\(2 \le n \le 50000\)),需要一一配对,使得白点在黑点的右下角,且曼哈顿距离和最小.题目保证有解. 分析 考虑最优解,我们可以交换任意一个配 ...
- 【HDU】2829 Lawrence
http://acm.hdu.edu.cn/showproblem.php?pid=2829 题意:将长度为n的序列分成p+1块,使得$\sum_{每块}\sum_{i<j} a[i]a[j]$ ...
- U-Boot命令大全(功能参数及用法)
U-Boot上电启动后,按任意键可以退出自动启动状态,进入命令行. U-Boot 2010.03 (Sep 25 2011 - 16:18:50) DRAM: 64 MB Flash: ...
- nodejs入门(一)
1.nodejs简介 Nodejs不是一个js应用.而是一个js运行平台. Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效 2. 1).Nodejs内置了一个HTTP模块 var ...