標籤:
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
iOS反向傳值--Block方法