您的位置:首页 > 移动开发 > IOS开发

ios-私人通讯录 页面间的跳转和传值

2015-09-06 22:16 507 查看
这个demo 有多个页面 并涉及顺传和逆传

而且还有一个第三方库的导入 来实现自定义提示消息的特效

利用代理来实现页面间的传值

一个页面代表一个controller

这次 ViewController 反而一句代码都没写



//
//  HMContact.h
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#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

//
//  HMContact.m
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMContact.h"

@implementation HMContact

+ (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone
{
HMContact *contact = [[HMContact alloc]init];

contact.name = name;
contact.phone = phone;

return contact;
}
@end


//
//  HMLoginViewController.h
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"

@interface HMLoginViewController : ViewController

@end

//
//  HMLoginViewController.m
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMLoginViewController.h"
#import "MBProgressHUD+MJ.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 *rmbPwsS;

@end

@implementation HMLoginViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *v =segue.destinationViewController;
v.navigationItem.title = [NSString stringWithFormat:@"%@的联系人",_accountField.text];
}
- (IBAction)login:(id)sender {
if ([_accountField.text isEqualToString:@"hm"]&&[_pwdField.text isEqualToString:@"123"]) {

//        UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//        UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"contacts"];
//        NSLog(@"%@",[vc class]);
//
//        [self.navigationController pushViewController:vc animated:YES];
[MBProgressHUD showMessage:@"正在登录中"];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"login2contact" sender:nil];
[MBProgressHUD hideHUD];
});

}else
{
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"账号密码错误" message:nil delegate:nil  cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//        [alert show];

[MBProgressHUD showError:@"账号密码错误"];
}
}

- (IBAction)rmbPwdSwitch:(UISwitch *)sender {
if (sender.isOn == NO) {
[_autoLoginS setOn:NO animated:YES];
// _autoLoginS.on = NO;
}
}

- (IBAction)autoLoginSwitch:(UISwitch *)sender {
if (sender.isOn == YES) {
[_rmbPwsS setOn:YES animated:YES];
// _autoLoginS.on = NO;
}
}

- (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)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

//_accountField.delegate = self;

}

- (void)textChange
{
//    if (_pwdField.text.length && _accountField.text.length ) {
//        _loginBtn.enabled = YES;
//    }
//    else
//    {
//        _loginBtn.enabled = NO;
//    }

_loginBtn.enabled = _accountField.text.length && _pwdField.text.length;
NSLog(@"%@",_accountField.text);
}

/*
#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


//
//  HMContachTableViewController.h
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HMContachTableViewController : UITableViewController

- (void)setName:(NSString *)name phone:(NSString *)phone;

@end

//
//  HMContachTableViewController.m
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMContachTableViewController.h"
#import "HMAddViewController.h"
#import "HMContact.h"
@interface HMContachTableViewController ()<UIActionSheetDelegate,HMAddViewControllerDelgegate>
@property (nonatomic,strong)NSMutableArray *contacts;
@end

@implementation HMContachTableViewController

- (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
{
HMAddViewController *vc = segue.destinationViewController;
// vc.contacts = self;
vc.delegate =self;
}

- (void)addViewController:(HMAddViewController *)add didAddContact:(HMContact *)contact
{
[self.contacts addObject:contact];

[self.tableView reloadData];
NSLog(@"%@",contact.name);
}

- (IBAction)loginout:(id)sender {
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"是否取消?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:nil, nil];

[sheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex) return;

[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 self.contacts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"contact";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell ==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
}

HMContact *contact  = self.contacts[indexPath.row];
cell.textLabel.text = contact.name;
cell.detailTextLabel.text = contact.phone;

return cell;
}


//
//  HMAddViewController.h
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "HMContachTableViewController.h"

@class HMAddViewController,HMContact;
@protocol HMAddViewControllerDelgegate <NSObject>

@optional
- (void)addViewController:(HMAddViewController *)add didAddContact:(HMContact *)contact;

@end

@interface HMAddViewController : UIViewController
@property(nonatomic,strong)HMContachTableViewController *contacts;

@property (nonatomic, weak) id<HMAddViewControllerDelgegate> delegate;
@end

//
//  HMAddViewController.m
//  私人通讯录
//
//  Created by YaguangZhu on 15/9/6.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "HMAddViewController.h"
#import "HMContachTableViewController.h"
#import "HMContact.h"

@interface HMAddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@end

@implementation HMAddViewController
- (IBAction)add:(id)sender {
[self.navigationController popViewControllerAnimated:YES];

// [self.contacts setName:_nameField phone:_phoneField];
HMContact *contact = [HMContact contactWithName:_nameField.text phone:_phoneField.text];
// 2.通知代理做事情
if ([_delegate respondsToSelector:@selector(addViewController:didAddContact:)]) {
[_delegate addViewController:self didAddContact:contact];
}

}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[_nameField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
[_phoneField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
[self textChange];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

//_accountField.delegate = self;

}

- (void)textChange
{
//    if (_pwdField.text.length && _accountField.text.length ) {
//        _loginBtn.enabled = YES;
//    }
//    else
//    {
//        _loginBtn.enabled = NO;
//    }

_addBtn.enabled = _nameField.text.length && _phoneField.text.length;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: