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;
}
调用方法相信大家都会啦,我就不多写了;
谢谢观看!