segue 个人通讯录 代理设置

1.login view controller:

//
//  loginViewController.m
//  segue
//
//  Created by chenvinci on 2016/12/23.
//  Copyright © 2016年 chenvinci. All rights reserved.
//

#import "loginViewController.h"

@interface loginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *account;
@property (weak, nonatomic) IBOutlet UITextField *key;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSNotificationCenter* notify = [NSNotificationCenter defaultCenter];
    [notify addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.account];
    [notify addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.key];
    [self textChange];
    // Do any additional setup after loading the view.
}

-(void)textChange{
    if (self.account.text.length>0 && self.key.text.length>0) {
        self.loginBtn.enabled = YES;
    }else {
        self.loginBtn.enabled = NO;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)login:(id)sender {
    
    [self.view endEditing:YES];
    //用户名和密码一致时,验证成功
    if (self.account.text  == self.key.text) {
        [self performSegueWithIdentifier:@"login2contacts" sender:nil];
    }else{
        //如果验证失败,弹出 密码错误 的警示
        UILabel* tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 100, 80, 60)];
        tipLabel.backgroundColor = [UIColor grayColor];
        tipLabel.text = @"密码错误";
        tipLabel.textColor  = [UIColor whiteColor];
        tipLabel.textAlignment = NSTextAlignmentCenter ;
        tipLabel.alpha = 1;
        [self.view addSubview:tipLabel];
        
        [UIView animateWithDuration:2.0 animations:^{
            tipLabel.alpha = 0;
        }completion:^(BOOL finished) {
            [tipLabel removeFromSuperview];
        }];
    }
}

-(void)dealloc{
    NSNotificationCenter*notify =[NSNotificationCenter defaultCenter];
    [notify removeObserver:self name:UITextFieldTextDidChangeNotification object:self.account];
    [notify removeObserver:self name:UITextFieldTextDidChangeNotification object:self.key];
}

#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 {
    UIViewController* des = segue.destinationViewController;
    des.navigationItem.title = [NSString stringWithFormat:@"%@的联系人列表",self.account.text];
}


@end

2.contact tableview controller:

//
//  contactTableViewController.m
//  segue
//
//  Created by chenvinci on 2016/12/24.
//  Copyright © 2016年 chenvinci. All rights reserved.
//

#import "contactTableViewController.h"
#import "addViewController.h"
#import "changeViewController.h"


@interface contactTableViewController ()
@property(nonatomic,strong) NSMutableArray* dataSourceArray;
@end

@implementation contactTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self dataSourceArray];
}

-(NSMutableArray*) dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"%@", segue.identifier);
        addViewController* addVC = segue.destinationViewController;
        addVC.delegate = self;

}
-(void)callBack:(NSDictionary *)infoDict{

    [self.dataSourceArray addObject:infoDict];

    //更新首页视图:
    [self.tableView reloadData];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return self.dataSourceArray.count;
        
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    NSDictionary *dic = [self.dataSourceArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic valueForKey:@"name"];
    cell.detailTextLabel.text = [dic valueForKey:@"tele"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    changeViewController* changeVC = [[changeViewController alloc]init];
    changeVC.delegate = self;
    
    [self.navigationController pushViewController:changeVC animated:YES];
    NSDictionary *dic = [self.dataSourceArray objectAtIndex:indexPath.row];
    changeVC.name.text = [dic valueForKey:@"name"];
    changeVC.tele.text = [dic valueForKey:@"tele"];
    changeVC.row = indexPath.row;
    
}
-(void)callBackFromChange:(NSDictionary *)infoDict index:(NSInteger)index{
    [self.dataSourceArray removeObjectAtIndex:index];
    [self.dataSourceArray insertObject:infoDict atIndex:index];
    
    //更新首页视图:
    [self.tableView reloadData];
}

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

3.add view controller

//
//  addViewController.h
//  segue
//
//  Created by chenvinci on 2016/12/23.
//  Copyright © 2016年 chenvinci. All rights reserved.
//

#import 

@protocol addViewContollerDelegate 

-(void) callBack:(NSDictionary* )infoDict;

@end

@interface addViewController : UIViewController
@property(nonatomic,weak) id delegate;
@end

//
//  addViewController.m
//  segue
//
//  Created by chenvinci on 2016/12/23.
//  Copyright © 2016年 chenvinci. All rights reserved.
//

#import "addViewController.h"

@interface addViewController ()
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *tele;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

@end

@implementation addViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}



- (IBAction)save:(id)sender {
    if ([self.delegate respondsToSelector:@selector(callBack:)]) {

        NSMutableDictionary* infoDict = [NSMutableDictionary dictionary];
        [infoDict setObject:self.name.text forKey:@"name"];
        
        [infoDict setObject:self.tele.text forKey:@"tele"];
        
        [self.delegate callBack:infoDict];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

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

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

4.change view controller

//
//  changeViewController.h
//  segue
//
//  Created by chenvinci on 2016/12/24.
//  Copyright © 2016年 chenvinci. All rights reserved.
//

#import 
@protocol changeViewContollerDelegate 

-(void) callBackFromChange:(NSDictionary* )infoDict index:(NSInteger)index;

@end


@interface changeViewController : UIViewController

@property (strong, nonatomic)  UITextField *name;
@property (strong, nonatomic)  UITextField *tele;
@property(assign,nonatomic) NSInteger row;
@property(nonatomic,weak) id delegate;

@end

//
//  changeViewController.m
//  segue
//
//  Created by chenvinci on 2016/12/24.
//  Copyright © 2016年 chenvinci. All rights reserved.
//

#import "changeViewController.h"

@interface changeViewController ()

@property(strong,nonatomic) UIButton* btn;

@end

@implementation changeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel* nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(74, 171, 42, 21)];
    nameLabel.text = @"姓名";
    [self.view addSubview:nameLabel];
    UILabel* teleLabel = [[UILabel alloc]initWithFrame:CGRectMake(74, 259, 42, 21)];
    teleLabel.text = @"电话";
    [self.view addSubview:teleLabel];

    [self btn];
    self.navigationItem.title = @"修改联系人";
    
    
    
}







-(UITextField*) name{
    if (!_name) {
        _name = [[UITextField alloc]initWithFrame:CGRectMake(124, 171, 139, 30)];
        _name.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_name];
    }
    return _name;
}
-(UITextField*) tele{
    if (!_tele) {
        _tele = [[UITextField alloc]initWithFrame:CGRectMake(124, 254, 139, 30)];
        _tele.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:_tele];
    }
    return _tele;
}

-(UIButton*)btn{
    if (!_btn) {
        _btn = [[UIButton alloc]initWithFrame:CGRectMake(114, 300, 156, 55)];
        [_btn setTitle:@"保存" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.view addSubview:_btn];
        [_btn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}
-(void)save{
    if ([self.delegate respondsToSelector:@selector(callBackFromChange:index:)]) {
        
        NSMutableDictionary* infoDict = [NSMutableDictionary dictionary];
        [infoDict setObject:self.name.text forKey:@"name"];
        
        [infoDict setObject:self.tele.text forKey:@"tele"];
        
        [self.delegate callBackFromChange:infoDict index:self.row];
    }
    [self.navigationController popViewControllerAnimated: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

你可能感兴趣的:(segue 个人通讯录 代理设置)