iOS 的传值处理(顺逆)

iOS 的传值处理(顺逆)

博客使用gitbook写的,可能在csdn的MarkDown上有部分不支持;欢迎访问原文:iOS 的传值处理(顺逆)

顺传:

将FirstViewController 里面textFiled里面的值传给SecondViewController里面的Label;首先在将FirstViewController里面创建有个textFiled编辑框,并且在里面创建一个按钮(push到SecondViewController

创建FirstViewController的按钮

  UIButton *button = [[UIButton alloc]init];
    button.frame = CGRectMake(100, 100, 200, 100);
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:button];

创建全局的编辑框

@property(nonatomic,strong)UITextField *textFiled;
    self.textFiled = [[UITextField alloc]initWithFrame:CGRectMake(20, 300, 300, 40)];
    self.textFiled.backgroundColor = [UIColor colorWithRed:0.3272 green:0.9068 blue:1.0 alpha:1.0];
    [self.view addSubview:self.textFiled];

点击按钮实现跳转

-(void)click{

    SecondViewController *pushView = [[SecondViewController alloc]init];
    pushView.textString = self.textFiled.text;
    [self.navigationController pushViewController:pushView animated:YES];

}

在SecondViewController的头文件里面设置一个全局变量,让外部可以访问;


#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property(nonatomic,strong)NSString *textString;

@end

在.m文件里面创建一个Button点击可以返回FirstViewController并且创建一个Label储存上一界面传入的值:

  self.view.backgroundColor = [UIColor lightGrayColor];
    UIButton *button = [[UIButton alloc]init];
    button.frame = CGRectMake(100, 100, 100, 50);
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 200, 100, 40)];
    //self.label=label;
    label.backgroundColor = [UIColor colorWithRed:0.8322 green:1.0 blue:0.2686 alpha:1.0];
    label.text = self.textString;//  属性传递得到文本
    [self.view addSubview:label];

这样子一个界面编辑的内容就可以传递到第二个界面的Label了;

逆传

界面的逆向传递比较复杂;在这里介绍一种最常用的传值(代理)方式。

思路:将第三个界面里面textFiled的内容传入第二个界面里面的Label里面显示;

【不多说了,小伙伴叫我去吃饭了,直接上代码】

创建按钮(跳转到第三个界面)

    UIButton *button = [[UIButton alloc]init];
    button.frame = CGRectMake(100, 300, 100, 50);
    [button setBackgroundColor:[UIColor orangeColor]];
    [button addTarget:self action:@selector(clickthree:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

在第三个界面ThreeViewController里面创建一个Button和一个textFiled输入框

    self.view.backgroundColor = [UIColor orangeColor];
    UIButton *button = [[UIButton alloc]init];
    button.frame = CGRectMake(100, 100, 100, 50);
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    -(UITextField *)textFiled{
    if (!_textFiled) {
        self.textFiled = [[UITextField alloc]initWithFrame:CGRectMake(20, 300, 300, 40)];
        self.textFiled.backgroundColor = [UIColor colorWithRed:0.3272 green:0.9068 blue:1.0 alpha:1.0];
        [self.view addSubview:self.textFiled];
    }
    return _textFiled;
}
到了关键步骤了:

首先在自己要监听的view里面设置代理(这里就在第三个界面里面设置代理)在.h文件里面

#import <UIKit/UIKit.h>
@class User;
@class ThreeViewController;
@protocol ThreeViewControllerDelegate <NSObject>
@optional
//-(void)addViewController:(ThreeViewController *)addViewContrioller didaddName:(NSString *)name andNumber:(NSString *)number;

-(void)addViewController:(ThreeViewController *)addViewContrioller addUser:(User *)User;
@end
@interface ThreeViewController : UIViewController
@property(nonatomic,weak)id<ThreeViewControllerDelegate> delegate;
@end

哦。。。差点忘了这里是用的模型,所以先创建一个模型,文件命名为User,将自己要传的数据封装进去。

#import <Foundation/Foundation.h>
@interface User : NSObject
@property(nonatomic,copy)NSString *name;
@end

在ThreeViewController里面让按钮遵循代理跳转(user头文件记得导入)

-(void)click:(UIButton *)sender{
   //  判断代理能否响应
//    if ([self.delegate respondsToSelector:@selector(addViewController:didaddName:andNumber:)]) {
//    [self.delegate addViewController:self didaddName:self.textFiled.text andNumber:self.textFiled.text];
//    }
//
    if ([self.delegate respondsToSelector:@selector(addViewController:addUser:)]) {
        User *users = [[User alloc]init];
        users.name = self.textFiled.text;
        [self.delegate addViewController:self addUser:users];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

回到SecondViewController第二个界面里面,首先让它遵守代理(记得导入user头文件)

@interface SecondViewController ()<ThreeViewControllerDelegate>

然后再声明一个可变数组,并且进行懒加载来存放模型数据:

@property(nonatomic,strong)NSMutableArray *users;

@implementation SecondViewController
- (NSMutableArray *)users{
    if (!_users) {
        _users = [NSMutableArray array];
    }
    return _users;
}

在点击调到第三个界面的按钮实现方法里面设置代理

-(void)clickthree:(id)sender{

    ThreeViewController *pushView = [[ThreeViewController alloc]init];
    pushView.delegate = self;
    [self.navigationController pushViewController:pushView animated:YES];

}```

最后调用代理方法拿到数据,这里的因为要把值给label所以要设置一个全局的label来储存内容
```bash
@property(nonatomic,weak)UILabel *label;
//  代理,拿到数据
-(void)addViewController:(ThreeViewController *)addViewContrioller addUser:(User *)User{
   // NSLog(@"%@__",User.name);
    [self.users addObject:User];
    self.label.text = User.name;
}

以上是在view里面进行传值,如果在UITableView里面进行传值;

一、某一行遵循代理(将后一个界面的值返回前面的某一行)返回昵称,姓名等
前面的设置代理不变。主要就在选中跳转的行里面设置该行遵循代理

 case 2:
        { TLNameViewController *pushCell = [[TLNameViewController alloc]init];
            //  跳转遵守代理
           ** pushCell.delegate = self;**
            [self.navigationController pushViewController:pushCell animated:YES ];
        }

然后再定义全局的cell来存放传进来的数据

  case 2:
            cell.textLabel.text=@"姓名";
            self.namecell=cell;    //获取当前传入的cell
         //   cell.detailTextLabel.text=@"修改";

最后再遵循代理接收数据

//  获取name数据
-(void)addViewController:(TLNameViewController *)ViewController addUserName:(TLUsers *)name{

   [self.users addObject:name];
   self.namecell.detailTextLabel.text = name.name;
  // NSLog(@"__%@",name.name);
   [self.tableView reloadData];
}

二、倘若像通讯录一样要全部传入真个tableView;那么要自己定义传入的行数;这个可以根据模型里面接收返回的数据增加;返回自定义的模型的行数

#pragma tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.users.count;

}

记得让跳转按钮遵守代理

 TLAddPassengerViewController *pushCell5 = [[TLAddPassengerViewController alloc]init];
    pushCell5.delegate = self;  //  设置代理
     [self.navigationController pushViewController:pushCell5 animated:YES ];

然后遵循代理接收数据:

//  获取User的数据
-(void)addViewController:(TLAddPassengerViewController *)viewController addUser:(TLUsers *)user
{

    [self.users addObject:user];
    [self.tableView reloadData];
}

最后显示数据:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        //  定义cell显示风格
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    }
    self.cell = cell;
    cell.backgroundColor =[UIColor colorWithRed:0.950 green:0.971 blue:0.980 alpha:1.000];
    //用view来画分割线

    UIView *customLine = [[UIView alloc] init];
    customLine.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, self.view.frame.size.width+55, 10);
    customLine.backgroundColor = [UIColor colorWithRed:0.888 green:0.865 blue:0.872 alpha:1.000];

    [cell.contentView addSubview:customLine];
    //  显示传入数据
    TLUsers * user = self.users[indexPath.row];
    cell.textLabel.text = user.passName;
    cell.detailTextLabel.text = user.passID;

    if (self.cell.detailTextLabel.text.length>7) {
        [self numberApperStar];
    }
    return cell;

这样传值就结束了。
有啥问题欢迎留言,希望可以和大家多多交流【处女篇,请多多关照、、、】

附带小功能:

当返回cell里面的数字太长了,或者是身份证手机号等为了加密处理
显示前面三位和最后四位,中间均用星号代替:

//  当数字大于7位,中间显示星号
-(void)numberApperStar{
    //星号字符串
    NSString *xinghaoStr = @"";
    NSString *idcardNumber = self.cell.detailTextLabel.text;
    //动态计算星号的个数
    for (int i  = 0; i < idcardNumber.length - 7; i++) {
        xinghaoStr = [xinghaoStr stringByAppendingString:@"*"];
    }
    //身份证号取前3后四中间以星号拼接
    idcardNumber = [NSString stringWithFormat:@"%@%@%@",[idcardNumber substringToIndex:3],xinghaoStr,[idcardNumber substringFromIndex:idcardNumber.length-4]];
     self.cell.detailTextLabel.text = idcardNumber;
  }

调用方法相信大家都会啦,我就不多写了;
谢谢观看!

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

推荐阅读更多精彩内容