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:@"123"]) { // 账号和密码正确 // 显示遮盖:只要做一些比较耗时的操作最好用遮盖 [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 = 1; CGFloat divideW = self.bounds.size.width; CGFloat divideY = self.bounds.size.height - divideH; self.divide.frame = CGRectMake(0, 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
没有失败, 只有放弃...