1.正向传值:
概念:RootViewController(RVC) (传向)—> SubViewController(SVC)
方法:正向传值又称属性传值,在SVC中定义专门用来传值的属性(如传字符串属性的值,则定义NSString类型属性)
RVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.string = tf.text;
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@property(nonatomic,retain) NSString * string;//定义一个属性,用来接收数据
SVC.m文件:
label.text = self.string;//在这里,将传进来的数据,赋给label
2.反向传值:
(1)使用对象传值:
概念:在SVC.h文件中定义RVC类型的对象作为属性,以方便调用/以赋值
RVC.h文件:
-(void)backValue:(NSString *)string color:(UIColor *)color;//声明一个方法用来进行传值
RVC.m文件:
-(void)backValue:(NSString *)string color:(UIColor *)color//定义返回参数的函数
{
label.text = string;//将参数的值赋给label
label.textColor = color; //将颜色赋给赋给lable的字体颜色
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.rvc = self;//让B持有A
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@property(nonatomic,retain) KGRootViewController * rvc;//创建一个Root对象
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.rvc backValue:tf.text color:[UIColor redColor]];//在销毁之前,做一些回传数据的事
[self dismissViewControllerAnimated:YES completion:nil];
}
(2)使用target/selector传值:
概念:在SVC.h文件中声明target/selector属性,以传递变量和方法,避免对象传值中信息完全暴露的危险
RVC.m文件:
-(void)backValue:(NSString *)string
{
label.text = string;//回传数据方法
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.target = self;//将回传对象进行指定
svc.selector = @selector(backValue:);//将回传方法,进行指定
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
//在这里定义两个属性,用来接收目标和方法,用来进行反向传值
@property(nonatomic,retain) id target; //接收要回传的对象
@property(nonatomic,assign) SEL selector;//接收回传数据的方法
SVC.m 文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//在销毁之前,将数据回传回去
//利用保存的target 和 action 来进行回传
if ([self.target respondsToSelector:self.selector]) {
[self.target performSelector:self.selector withObject:tf.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
(3)使用协议代理传值:
概念:要SVC—>RVC,则需要RVC遵守SVC的协议,实现协议中的方法,才能将值反向传递
RVC.m文件:
#import "KGSubViewController.h"//因为协议制定在B的.h文件里,所以在导入.h文件时,协议也一起导入
//实现协议 方法
-(void)backValue:(NSString *)string
{
label.text = string;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.delegate = self;//让A同意B所提出的协议条件
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@protocol BackValue //在B页面里,制定一个协议
-(void)backValue:(NSString *)string;//回传数据的协议方法
@end
@property(nonatomic,weak) id < BackValue > delegate;
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.delegate backValue:tf.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
(4)使用系统自带的completion block传值:
概念:利用系统自带的completion block函数进行传值
RVC.h文件:
@property(nonatomic,retain) UILabel * label;//用来反向接收数据
RVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.rvc = self;
//OC中,block用来去指向一个匿名的语句块,从而可以当成函数还调用该语句块
//这个方法的第三个参数是一个block,意思说,当弹出来的svc显示完成后,执行block里的内容
[self presentViewController:svc animated:YES completion:^{
svc.textField.text = self.label.text;//正向传值
}];
}
SVC.h文件:
@property(nonatomic,retain) UITextField * textField;//用来正向接收数据
@property(nonatomic,retain) KGRootViewController * rvc;//一个反向数据
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self dismissViewControllerAnimated:YES completion:^{
self.rvc.label.text = self.textField.text;}];
}
(5)使用自定义的block传值:
概念:因为block相当于匿名函数指针,是一段方法的代码块,所以只要有这个指针就可以调用该方法,而这个方法具体要实现什么功能可以视情况而定,相实现什么方法就定义什么方法
RVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
KGSubViewController * svc = [[KGSubViewController alloc]init];
svc.block = ^(NSString * string){//给block赋值,做用是让svc知道block指向的代码块的功能
label.text = string;};
[self presentViewController:svc animated:YES completion:nil];
}
SVC.h文件:
@property(nonatomic,copy)void (^block)(NSString *);//定义一个block的属性
SVC.m文件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(self.block){
self.block(tf.text);//执行block
}
[self dismissViewControllerAnimated:YES completion:nil];
}