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文件聲明以下代碼:code
#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