普遍传值方式如下:
1.委托delegate方式;
2.通知notification方式;
3.block方式;
4.UserDefault或者文件方式;
5.单例模式方式;
6.通过设置属性,实现页面间传值
着重说一下block方式:
block介绍:http://blog.****.net/totogo2010/article/details/7839061
链接一篇描述block回调挺有意思的文章: http://blog.****.net/mobanchengshuang/article/details/11751671
分析:
在B试图控制器中,定义一个block,参数为字符串
//SecondViewController.h
typedef void (^ablock)(NSString *str); //SecondViewController.h
@property (nonatomic, copy) ablock block;
在B试图控制器中,当输入名字,点击对应的确定按钮后
self.block(self.nameTextField.text);
[self dismissViewControllerAnimated:YES completion:nil];
在A视图显示,回调block
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@'SecondViewController' bundle:nil];
[self presentViewController:second animated:YES completion:nil]; second.block = ^(NSString *str){
self.nameLabel.text = str;
};
------------------------------------------华丽的分割线----------------------------------------------------
以上就是block简单传值,通知NSNotification比较简单:
在需要接收值得位置先添加监听:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextFiled:) name:@"NNN" object:nil];
添加个方法来输出一下,看所传的值:
- (void)changeTextFiled:(NSNotification *)noti{
NSLog(@"%s--value:%@",__func__,noti.userInfo[@"value"]);
}
然后在传值的主动方发送通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NNN" object:nil userInfo:@{@"value": mess}];
这样就可以了,但是切记,要在不用通知的位置注销通知:
[[NSNotificationCenter defaultCenter] removeObserver:self];
这就是一个完整的通知监听模式。
引用出自:iOS页面传值方式