首页 > 代码库 > 03通讯录(逆传)

03通讯录(逆传)

 

技术分享技术分享

 

////  XMGLoginViewController.m//  小码哥通讯录#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.创建segue 3.设置来源控制器segue.sourceViewController = self 4.创建目的控制器,segue.destinationViewController = 目的控制器 5.[self prepareForSegue]跳转之前的准备操作 6.[segue perform] 7.判断下segue的类型,如果是push,拿到导航控制器push [self.navigationController pushViewController:segue.destinationViewController animated:YES]; */@implementation XMGLoginViewController// 在执行跳转之前的时候调用- (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"]) { // 输入正确                        // 直接跳转            // 跳转到联系人界面            [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.        // 给文本框添加监听器,及时监听文本框内容的改变    [_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

 

////  XMGContactViewController.m//  小码哥通讯录////  Created by xiaomage on 15/6/12.//  Copyright (c) 2015年 xiaomage. All rights reserved.//#import "XMGContactViewController.h"#import "XMGAddViewController.h"#import "XMGContact.h"@interface XMGContactViewController ()<UIActionSheetDelegate>@end@implementation XMGContactViewController- (void)setContact:(XMGContact *)contact{    _contact = contact;    NSLog(@"%@",contact.name);}// 跳转之前的时候调用- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{        // 给添加控制器传递联系人控制器属性    XMGAddViewController *addVc = segue.destinationViewController;        addVc.contactVC = self;}// 点击注销的时候调用- (IBAction)logout:(id)sender {        // 弹出actionSheet    UIActionSheet *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];        // Uncomment the following line to preserve selection between presentations.    // self.clearsSelectionOnViewWillAppear = NO;        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.    // self.navigationItem.rightBarButtonItem = self.editButtonItem;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {#warning Potentially incomplete method implementation.    // Return the number of sections.    return 0;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {#warning Incomplete method implementation.    // Return the number of rows in the section.    return 0;}/*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];        // Configure the cell...        return cell;}*//*// Override to support conditional editing of the table view.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    // Return NO if you do not want the specified item to be editable.    return YES;}*//*// Override to support editing the table view.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    if (editingStyle == UITableViewCellEditingStyleDelete) {        // Delete the row from the data source        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    } else if (editingStyle == UITableViewCellEditingStyleInsert) {        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view    }   }*//*// Override to support rearranging the table view.- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {}*//*// Override to support conditional rearranging of the table view.- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {    // Return NO if you do not want the item to be re-orderable.    return YES;}*//*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end
////  XMGContact.h//  小码哥通讯录#import <Foundation/Foundation.h>@interface XMGContact : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) NSString *phone;+ (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone;@end
////  XMGContact.m//  小码哥通讯录#import "XMGContact.h"@implementation XMGContact+ (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone{    XMGContact *c = [[self alloc] init];        c.name = name;    c.phone = phone;        return c;}@end

 

////  XMGAddViewController.h//  小码哥通讯录#import <UIKit/UIKit.h>@class XMGContactViewController;@interface XMGAddViewController : UIViewController@property (nonatomic, strong) XMGContactViewController *contactVC;@end

 

////  XMGAddViewController.m//  小码哥通讯录#import "XMGAddViewController.h"#import "XMGContact.h"#import "XMGContactViewController.h"@interface XMGAddViewController ()@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *phoneField;@property (weak, nonatomic) IBOutlet UIButton *addBtn;@end@implementation XMGAddViewController// 点击添加的时候调用- (IBAction)add:(id)sender {    // 0.把文本框的值包装成联系人模型    XMGContact *c = [XMGContact contactWithName:_nameField.text phone:_phoneField.text];        NSLog(@"%@",_contactVC);        // 1.把联系人传递联系人控制器    _contactVC.contact = c;            // 2.回到联系人控制器    [self.navigationController popViewControllerAnimated:YES];    }- (void)viewDidLoad {    [super viewDidLoad];        // 给文本框添加监听器,及时监听文本框内容的改变    [_nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];    [_phoneField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];            }- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    // 主动弹出姓名文本框    [_nameField becomeFirstResponder];}// 任一一个文本框的内容改变都会调用- (void)textChange{    _addBtn.enabled = _nameField.text.length && _phoneField.text.length;    }@end

 

03通讯录(逆传)