iOS--开发中的六大传值(OC中的常用传值)

六大传值--(属性/代理/通知/KVO/KVC/Tag/单例/Block/全局)

先创建两个ViewController:

1,先在Appdelegate.m里设置一个AViewController.m 为rootViewController,并创建一个BViewController

在AViewController.m中导入BViewController.h,,,创建一个SingleOne继承于NSObject作为单例类

import "AViewController.h"

import "BViewController.h"

//单例

import "SingleOne.h"

//代理传值,遵循协议Nextprotocol
@interface AViewController ()<UITextFieldDelegate,Nextprotocol>
{
BViewController *bView;
}

@end

@implementation AViewController

  • (void)viewDidLoad {
    [superviewDidLoad];

    self.title =@"AViewController";
    self.view.backgroundColor = [UIColorwhiteColor];

    //作一个跳转button
    UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithTitle:@"跳转"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(touchAction:)];
    self.navigationItem.rightBarButtonItem = item;

//声明一个aTextFiled
UITextField *aTextField = [[UITextFieldalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-150, [UIScreenmainScreen].bounds.size.height/2-25,300, 30)];
aTextField.layer.borderWidth =2;
aTextField.layer.borderColor = [UIColorgrayColor].CGColor;
aTextField.layer.masksToBounds =YES;
aTextField.tag = 1000;
aTextField.delegate = self;
[self.viewaddSubview:aTextField];

// aTextField.text = @"李涛你个混蛋";

//反正是KVO时就一定要放在viewdidLoad这里,不会跳两第二次时会报错,说这个bView已经消亡了但还有人在监听它
bView = [[BViewController alloc]init];

}

  • (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    [textField resignFirstResponder];
    return YES;

}

-(void)touchAction:(UIButton *)sender
{
//用tag传值
UITextField *aTextFiled = (UITextField *)[self.viewviewWithTag:1000];
// NSLog(@"aTextFiled.text = %@",aTextFiled.text);
// BViewController *bView = [[BViewControlleralloc]init];

/*-------------------单例传值:从A---->>B  ----------------------*/
    SingleOne *onlyOne = [SingleOneshareData];
//让单例作为中介去接收A页面要传的值,
    onlyOne.value = aTextFiled.text;


/*-------------------block传值:从B---->>A  -------------------*/
[bView setMblock:^(NSString *string){ //注意类型,也可以用id
    aTextFiled.text = string;
}];

bView.mblock = ^(id string){
    aTextFiled.text = string;
};


/*---------------------属性传值:从A--->>B  --------------------*/
bView.text1 = aTextFiled.text;




/*---------------------KVC传值-------------------------*/
[bView setValue:aTextFiled.textforKey:@"_name"];
[bView setValue:aTextFiled.textforKey:bView.text1];   //或者



/*---------------------通知传值 B--->>A ------------------------*/
//

如果是从A传到B的话,这里写发通知,B.m里要创建一个init方法,在里面写监听并在里面创建接收容器才能成功(因为程序先执行init方法再到viewDidLoad方法,当传值过去时在init就开始监听,如果这里没有创建textField接收,那就传不过去了,所以要在init里同时创建接收器(生命周期的问题));
//这里是从B到A:注册监听:从B传到A话因为从A跳转到B时已经创建好A了,所以
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(aAction:)name:@"amessage"object:nil];

/*---------------------代理传值 B--->>A  -------------------*/
bView.delegate = self;   //设置代理

/---------------------KVO回传值 B--->>A-------------------/
//注册观察者:如果用了KVO一定要把bView的初始化放在上面的viewDidLoad里
[bView addObserver:self forKeyPath:@"test1" options:NSKeyValueObservingOptionNew context:nil];
//把更改之后的值提供给观察者的回调方法

[self.navigationControllerpushViewController:bView animated:YES];

}

pragma mark============代理传值:实现协议方法==============

-(void)transferString:(NSString *)string
{
UITextField *aTextField = (UITextField *)[self.viewviewWithTag:1000];

aTextField.text = string;

}

pragma mark -----------通知回调方法实现--------------

-(void)aAction:(NSNotification *)sender
{
UITextField *aField = (UITextField *)[self.viewviewWithTag:1000];
aField.text = sender.userInfo[@"akey"];

}

pragma mark ------------- KVO回调 ------------------

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
UITextField *aTextField = (UITextField *)[self.view viewWithTag:1000];

//此处监听键路径key的值有没有发生变化
if ([keyPath isEqualToString:@"text1"]) {  //如果观察的对象是text1
    aTextField.text = bView.text1;
}

}

//移除通知的监听者 和 KVO的观察者(如果不移除,会变成野指针)
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"amessage" object:nil]; //移除通知监听者

[bView removeObserver:self forKeyPath:@"text1"];   //移除KVO观察者

}

  • (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

@end

BVIewController.h里

//
// BViewController.h
// test传值
//
// Created by ibokan on 16/3/16.
// Copyright © 2016年谭其伟. All rights reserved.
//

import <UIKit/UIKit.h>

pragma mark ======Block传值=========

typedef void(^TransferValue) (NSString *string);

pragma mark---------代理传值-------------

//声明一个协议
@protocol Nextprotocol <NSObject>
//协议里的方法(让遵循该协议的类实现,自己不实现)
-(void)transferString:(NSString *)string;

@end

@interface BViewController : UIViewController
{
//KVC传值需要有一个实例变量作为key
NSString *_name;
}

//把block作为属性
@property(nonatomic,copy)TransferValue mblock;

//属性传值
@property(nonatomic,readwrite,strong)NSString *text1;

pragma mark---------代理传值-------------

//有一个遵循协议的代理属性
@property(nonatomic,readwrite,strong)id<Nextprotocol>delegate;

@end

BViewController.m里

//
// BViewController.m
// test传值
//
// Created by ibokan on 16/3/16.
// Copyright © 2016年谭其伟. All rights reserved.
//

import "BViewController.h"

//单例

import "SingleOne.h"

@interface BViewController ()<UITextFieldDelegate>
{
UITextField *bTextFiled;
}
@end

@implementation BViewController

  • (void)viewDidLoad {
    [superviewDidLoad];

    self.title =@"BViewController";

    self.view.backgroundColor = [UIColorredColor];

    //声明一个返回button
    UIButton *aButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
    aButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50,150, 100, 30);
    [aButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
    [aButton addTarget:selfaction:@selector(backAction:)forControlEvents:UIControlEventTouchUpInside];
    [aButton setTitle:@"返回"forState:UIControlStateNormal];
    [self.viewaddSubview:aButton];

//使用全局传值:声明一个bTextFiled
bTextFiled = [[UITextFieldalloc]initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width/2-150, [UIScreen mainScreen].bounds.size.height/2-100,300, 30)];
bTextFiled.layer.borderColor = [UIColorblackColor].CGColor;
bTextFiled.layer.borderWidth =2;
//_btextFiled.delegate = self;
bTextFiled.text = [NSStringstring];  //相当于nil
bTextFiled.layer.masksToBounds =YES;
[self.viewaddSubview:bTextFiled];

pragma mark ------单例传值接收:再间接传给B页面-----------------

bTextFiled.text = [SingleOneshareData].value;

pragma mark ++++++++++++属性传值++++++++++++++++

bTextFiled.text =self.text1;

//KVC传值
self.text1 =_name;
bTextFiled.text =self.text1;

pragma mark ==============通知传值:回传=====================

//发送通知
[[NSNotificationCenterdefaultCenter] postNotificationName:@"amessage"object:selfuserInfo:@{@"akey":bTextFiled.text}];

}

-(void)backAction:(UIButton *)sender
{

/*-------------block传值要写在方法里----------------*/
_mblock(bTextFiled.text);
self.mblock(bTextFiled.text);



/*---------------代理传值-------------------------*/
if (self.delegate && [self.delegateconformsToProtocol:@protocol(Nextprotocol)]) {
    [self.delegatetransferString:bTextFiled.text];
}



[self.navigationControllerpopViewControllerAnimated:YES];

}

  • (void)didReceiveMemoryWarning {
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

@end

单例类:

SingleOne.h

// SingleOne.h
// test传值
//
// Created by ibokan on 16/3/16.
// Copyright © 2016年谭其伟. All rights reserved.
//

import <Foundation/Foundation.h>

@interface SingleOne : NSObject

//创建一个单例//如果在单线程里可以用nonatomic,如果在多线程里一定要用atomic,保证是只有一个在调用,不然在多线程里面如果多个方法调用修改单例类里的属性时会冲突
@property(atomic,readwrite,strong)NSString *value;

//声明一个类方法
+(SingleOne *)shareData;

@end

SingleOne.m里

//
// SingleOne.m
// test传值
//
// Created by ibokan on 16/3/16.
// Copyright © 2016年谭其伟. All rights reserved.
//

import "SingleOne.h"

//声明一个表态实例对象,让其为空
static SingleOne *onlyOne =nil;

@implementation SingleOne

//实现方法,判断是否为空,是就创建一个全局实例给它
+(SingleOne *)shareData
{
if (onlyOne ==nil) {
onlyOne = [[SingleOnealloc]init];
}
returnonlyOne;
}

//避免alloc/new创建新的实例变量--->增加一个互斥锁
+(id)allocWithZone:(struct_NSZone *)zone
{
@synchronized(self) {
if (onlyOne ==nil) {
onlyOne = [superallocWithZone:zone];
}
}
returnonlyOne;
}

//避免copy,需要实现NSCopying协议
-(id)copyWithZone:(NSZone *)zone
{

return self;

}

@end

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

推荐阅读更多精彩内容