当前位置: 移动技术网 > 移动技术>移动开发>IOS > 汇总ios开发逆向传值的方法

汇总ios开发逆向传值的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios的逆向传值有很多种方法,下面来总结几种常用的传值方式(只贴相关代码):

第一种:代理传值
第二个控制器:

@protocol wjsecondviewcontrollerdelegate <nsobject>
- (void)changetext:(nsstring*)text;
@end
 @property(nonatomic,assign)id<wjsecondviewcontrollerdelegate>delegate;

- (ibaction)buttonclick:(uibutton*)sender {
_str = sender.titlelabel.text;
[self.delegate changetext:sender.titlelabel.text];
[self.navigationcontroller popviewcontrolleranimated:yes];
}

第一个控制器:

- (ibaction)pushtosecond:(id)sender {
wjsecondviewcontroller *svc = [[wjsecondviewcontroller alloc]initwithnibname:@"wjsecondviewcontroller" bundle:nil];
svc.delegate = self;
svc.str = self.navigationitem.title;
[self.navigationcontroller pushviewcontroller:svc animated:yes];
[svc release];
}
- (void)changetext:(nsstring *)text{
self.navigationitem.title = text;
}

第二种:通知传值
第一个控制器:

 //注册监听通知
 [[nsnotificationcenter defaultcenter] addobserver:self     selector:@selector(limitdataformodel:) name:@"nov" object:nil];
- (void)limitdataformodel:(nsnotification *)noti{
self.gamesinfoarray = noti.object;
}

第二个控制器:

//发送通知
 [[nsnotificationcenter defaultcenter]   postnotificationname:@"nov" object:gamearray];

第三种:单例传值
single是一个单例类,并且有一个字符串类型的属性titlename
在第二个控制器:

- (ibaction)buttonclick:(uibutton*)sender {
single *single = [single sharedsingle];
single.titlename = sender.titlelabel.text;
[self.navigationcontroller popviewcontrolleranimated:yes];
}

第一个控制器:

- (void)viewwillappear:(bool)animated{
[super viewwillappear:animated];
single *single = [single sharedsingle];
self.navigationitem.title = single.titlename;
}

第四种:block传值
第二个控制器:

@property (nonatomic,copy) void (^changetext_block)(nsstring*);
- (ibaction)buttonclick:(uibutton*)sender {
_str = sender.titlelabel.text;
self.changetext_block(sender.titlelabel.text);
[self.navigationcontroller popviewcontrolleranimated:yes];
}

第一个控制器:

- (ibaction)pushtosecond:(id)sender {
wjsecondviewcontroller *svc = [[wjsecondviewcontroller alloc]initwithnibname:@"wjsecondviewcontroller" bundle:nil];
svc.str = self.navigationitem.title;
[svc setchangetext_block:^(nsstring *str) {
  >self.navigationitem.title = str;
}];
[self.navigationcontroller pushviewcontroller:svc animated:yes];
}

第五种:extern传值
第二个控制器:

 extern nsstring *btn;
- (ibaction)buttonclick:(uibutton*)sender {
btn = sender.titlelabel.text;
[self.navigationcontroller popviewcontrolleranimated:yes];
}

第一个控制器:

nsstring *btn = nil;
- (void)viewwillappear:(bool)animated{
[super viewwillappear:animated];
self.navigationitem.title = btn;
}

第六种:kvo传值
第一个控制器:

- (void)viewdidload {
[super viewdidload];
 _vc =[[secondviewcontroller alloc]init];
//self监听vc里的textvalue属性
[_vc addobserver:self forkeypath:@"textvalue" options:0 context:nil];  
}

第二个控制器:

- (ibaction)buttonclicked:(id)sender {
self.textvalue = self.textfield.text;
[self.navigationcontroller popviewcontrolleranimated:yes];
}

其实还有很多种传值方式,比如说nsuserdefaults,先把数据保持在本地,再读取,或者写入plist及其它类型的文件再读取等等许多方式,在这里就不一一列举了!这些代码写的时间比较久了,今天整理了一下,还比较乱,有什么不对或不足的地方请见谅!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网