博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
谈谈ios传值方式(属性传值、代理传值、Block传值、单例传值、通知传值、KVC传值)
阅读量:4218 次
发布时间:2019-05-26

本文共 3174 字,大约阅读时间需要 10 分钟。

一下所有的演示由:AViewController 和 BViewController 完成简写(AVC和BVC)

所有传值合集github链接:https://github.com/TianYou899/valueset

1.属性传值(AVC代码传到BVC代码):

AVC 导入  BVC

BVC声明属性

//接收值@property (nonatomic, copy) NSString * string;

BViewController * bVC = [[BViewController alloc]init];    bVC.string = self.aTextField.text;    [self.navigationController pushViewController:bVC animated:YES];
2.代理传值(BVC传到AVC):

BVC:

//定义代理@protocol delegateCZ 
// 代理方法- (void)transferString:(NSString *)string;@end@interface BViewController : UIViewController// 代理属性@property (nonatomic, weak) id
delegate;
 // 判断有没用遵循代理 有就执行    if (self.delegate && [self.delegate conformsToProtocol:@protocol(delegateCZ)]) {        [self.delegate transferString:self.bTextField.text];    }        [self.navigationController popToRootViewControllerAnimated:YES];

 AVC 
@interface AViewController ()
BViewController * bVC = [[BViewController alloc]init];    // 遵循代理    bVC.delegate = self;
// 从 B 从到A// 执行代理方法- (void)transferString:(NSString *)string {        self.aTextField.text = string;}
3.Block传值(BVC传到AVC):
BVC

// 定义一个blocktypedef void (^blockCZ)(NSString * string);@interface BViewController : UIViewController// block 属性@property (nonatomic,copy) blockCZ  block;
/**          Blcok 传值 B传到A     注意:因为A页面堆过来的时候已经初始化了B 所有不能从A传到B(这种情况下硬传的话 会造成循环引用)     */    _block(self.bTextField.text);    [self.navigationController popToRootViewControllerAnimated:YES];
AVC

BViewController * bVC = [[BViewController alloc]init];    [bVC setBlock:^(NSString * string){        self.aTextField.text = string;    }];        [self.navigationController pushViewController:bVC animated:YES];
4.单例传值(AVC传到BVC)

声明单例类.h

@interface DanLi : NSObject@property (atomic, copy) NSString *value;+ (DanLi *)sharedDanLi;@end
声明单例类.m
#import "DanLi.h"static DanLi *danli = nil;@implementation DanLi//实现方法,判断是否为空,是就创建一个全局实例给它+ (DanLi *)sharedDanLi {        if (danli == nil) {        danli = [[DanLi alloc] init];    }    return danli;}//避免alloc/new创建新的实例变量+ (id)allocWithZone:(struct _NSZone *)zone {        @synchronized(self) {        if (danli == nil) {            danli = [super allocWithZone:zone];        }    }    return danli;}//避免copy,需要实现NSCopying协议- (id)copyWithZone:(NSZone *)zone {    return self;}@end

 
5.通知传值(BVC传到AVC)

AVC

// 接收通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tzAction:) name:@"tz" object:nil];
// 回调通知- (void)tzAction:(NSNotification *)sender {        self.aTextField.text = sender.userInfo[@"key"];}
BVC

[[NSNotificationCenter defaultCenter] postNotificationName:@"tz" object:nil userInfo:@{@"key":self.bTextField.text}];        [self.navigationController popToRootViewControllerAnimated:YES];
6.KVC传值(AVC传到BVC)

AVC

BViewController * bVC = [[BViewController alloc]init];        //KVC 传值:这里只能传A传到B (因为 B在A页面提前初始化)    //这里forkey 一定要和B 属性名字一致 (也可以用@"_string")因为是属性    // 给B属性string  赋值    [bVC setValue:self.aTextField.text forKey:@"string"];        [self.navigationController pushViewController:bVC animated:YES];
BVC

//接收值@property (nonatomic, copy) NSString * string;
// KVC 接收值    self.bTextField.text = self.string;
所有传值合集github链接:https://github.com/TianYou899/valueset

转载地址:http://xztmi.baihongyu.com/

你可能感兴趣的文章
C++ 枚举声明 enum 和 enum class
查看>>
Python optionParser模块的使用方法
查看>>
android 消灭星星出错
查看>>
PyCharm 教程(三)Hello world!
查看>>
PyCharm: 显示源码行号
查看>>
cocos2dx使用第三方字库.ttf,需要注意的事项
查看>>
cocos2dx 音频模块分析(4): 音效部分
查看>>
cocos2dx 音频模块分析(5): 音效部分
查看>>
19、Cocos2dx 3.0游戏开发找小三之Action:流动的水没有形状,漂流的风找不到踪迹、、、
查看>>
cocos2.X版本lua端使用定时器的方法
查看>>
lua math.fmod使用注意小数问题
查看>>
lua 时间转化
查看>>
lua学习笔记之五(Lua中的数学库)
查看>>
dos: tree命令生成目录结构
查看>>
Managing Projects from the Command Line(android官网文档)
查看>>
Android项目自动生成build.xml,用Ant打包
查看>>
CCLayer注册lua回调函数setTouchPriority失效
查看>>
cocos2dx左下角三行数值意义
查看>>
LUA modue require package 区别
查看>>
package.loaded
查看>>