标签:
RootViewController代码如下:
#import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #define kDebugPrint NSLog(@"%s",__func__) @interface RootViewController () { UILabel *_label; } @end @implementation RootViewController /* 正向传值 创建第一个界面 通过第一个界面跳转到第二个界面 如果由第一个界面向第二个界面 进行传值 正向传值 属性传值 第二张向第一张界面传值 反向传值 下级界面向上一级界面传值---》反向传值 反向传值方式: 1.代理传值 下级界面要把textField的内容 传给 上一级,这时下级界面就可以委托上级界面 修改 label的值 第二个界面(主动方) 可以制定一个协议 规范代理的行为, 第一个界面(被动方) 遵守协议 作为 代理 2.单例传值 1.系统单例 2.自定义单例 3.通知传值 4.NSUserDefaults 5.block传值 */ - (void)viewDidLoad { [super viewDidLoad]; [self showUI]; } - (void)showUI { self.view.backgroundColor = [UIColor grayColor]; _label = [MyControl creatLabelWithFrame:CGRectMake(0, 30, 300, 30) text:@"XXX"]; _label.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_label]; UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 200, 300, 50) target:self sel:@selector(btnClick:) tag:201 image:nil title:@"切换到第二张"]; [self.view addSubview:button]; } - (void)btnClick:(UIButton *)btn { //每次点击按钮 都会创建一个新的第二张对象 SecondViewController *svc = [[SecondViewController alloc] init]; [svc setMyBlock:^(NSString *textStr) { _label.text=textStr; }]; [self presentViewController:svc animated:YES completion:nil]; [svc release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
SecondViewController点h文件声明如下代码:
#import <UIKit/UIKit.h> //----------- typedef void(^ChangeTextBlock)(NSString *textStr); @interface SecondViewController : UIViewController { //void(^myBlock)(NSString *textStr); ChangeTextBlock _myBlock; } //修改和获取 -(void)setMyBlock:(ChangeTextBlock)block; -(ChangeTextBlock) myBlock; @end
#import "SecondViewController.h" #import "MyControl.h" #define kDebugPrint NSLog(@"%s",__func__) @interface SecondViewController () { UITextField *_textField; } @end @implementation SecondViewController - (void)dealloc { kDebugPrint; [_myBlock release]; [super dealloc]; } -(void)setMyBlock:(ChangeTextBlock)block{ if (_myBlock!=block) { [_myBlock release]; _myBlock=[block copy];//拷贝block } } -(ChangeTextBlock) myBlock{ return _myBlock; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; [self showUI]; } - (void)showUI { UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(10, 30, 300, 30) target:self sel:@selector(btnClick:) tag:301 image:nil title:@"返回"]; [self.view addSubview:button]; UIButton *button2 = [MyControl creatButtonWithFrame:CGRectMake(10,200 , 300, 30) target:self sel:@selector(btnClick2:) tag:302 image:nil title:@"传值"]; [self.view addSubview:button2]; _textField = [MyControl creatTextFieldWithFrame:CGRectMake(10, 100, 300, 30) placeHolder:nil delegate:nil tag:100]; [self.view addSubview:_textField]; } //收键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_textField resignFirstResponder]; } - (void)btnClick:(UIButton *)btn { //返回上一级 [self dismissViewControllerAnimated:YES completion:nil]; } - (void)btnClick2:(UIButton *)btn { //点击传值 去委托block执行修改第一个界面label的值 if (self.myBlock) { //_myBlock(_textField.text); //等价于上一句 self.myBlock(_textField.text); }else{ NSLog(@"没有传入block"); } } @end
标签:
原文地址:http://my.oschina.net/u/2410306/blog/523969