新建一個appdelegateapp
接着新建一個RootViewController SecondViewControlleratom
#import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #define kDebugPrint NSLog(@"%s",__func__) //遵照協議 @interface RootViewController ()<ChangeTextDelegate>//遵照協議 { UILabel *_label; } @end @implementation RootViewController /* 正向傳值 建立第一個界面 經過第一個界面跳轉到第二個界面 若是由第一個界面向第二個界面 進行傳值 正向傳值 屬性傳值 第二張向第一張界面傳值 反向傳值 下級界面向上一級界面傳值---》反向傳值 反向傳值方式: 1.代理傳值 第二個界面要把textField的內容 傳給 第一個界面, 第一個界面 能夠修改 label的值 這時第二個界面就能夠 委託 第一個界面 修改 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]; } #pragma mark - 代理實現協議方法 - (void)changeLabelTextWithString:(NSString *)string { _label.text = string; } - (void)btnClick:(UIButton *)btn { //每次點擊按鈕 都會建立一個新的第二張對象 SecondViewController *svc = [[SecondViewController alloc] init]; svc.delegate = self;//設置代理 [self presentViewController:svc animated:YES completion:nil]; [svc release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#import <UIKit/UIKit.h> //主動方制定協議 //規範代理的行爲 @protocol ChangeTextDelegate <NSObject> - (void)changeLabelTextWithString:(NSString *)string; @end //==========================上面是協議的格式形式 @interface SecondViewController : UIViewController //設置代理 弱引用 @property (nonatomic,assign) id <ChangeTextDelegate>delegate; @end
#import "SecondViewController.h" #import "MyControl.h" #define kDebugPrint NSLog(@"%s",__func__) @interface SecondViewController () { UITextField *_textField; } @end @implementation SecondViewController - (void)dealloc { kDebugPrint; [super dealloc]; } - (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 { //傳值 把值傳給第一個界面 if ([self.delegate respondsToSelector:@selector(changeLabelTextWithString:)]) { //通知代理 調用方法 而且把_textField.text傳給代理 //代理修改label的值 [self.delegate changeLabelTextWithString:_textField.text]; }else { NSLog(@"代理沒有實現方法"); } } @end