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 ()<UITableViewDataSource,UITableViewDelegate,addViewContollerDelegate,changeViewContollerDelegate>
@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 <UIKit/UIKit.h>

@protocol addViewContollerDelegate <NSObject>

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

@end

@interface addViewController : UIViewController
@property(nonatomic,weak) id<addViewContollerDelegate> 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 <UIKit/UIKit.h>
@protocol changeViewContollerDelegate <NSObject>

-(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<changeViewContollerDelegate> 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容