UI传值

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 <NSObject>//在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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这两个方法都可以传值 Button自己的跳转方法 又叫模态(突然间跳到另一个界面用这个方法) 第一页条第二页用[s...
    简单erwtger阅读 221评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,688评论 19 139
  • 禅与 Objective-C 编程艺术 (Zen and the Art of the Objective-C C...
    GrayLand阅读 1,807评论 1 10
  • 1.正向传值 属性传值在B类中定义属性用于接收A类传来的数据 2.反向传值(回调) 1)利用对象反向传值 将A类对...
    ScaryMonsterLyn阅读 1,488评论 0 0
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 12,511评论 6 13

友情链接更多精彩内容