新建一個空工程 在appdelegate中的.h填寫以下代碼app
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; //保存字符串 @property (nonatomic,copy) NSString *labelStr; @end
在RootViewController以下代碼:atom
#import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #import "AppDelegate.h" #define kDebugPrint NSLog(@"%s",__func__) @interface RootViewController () { UILabel *_label; } @end @implementation RootViewController /* 正向傳值 建立第一個界面 經過第一個界面跳轉到第二個界面 若是由第一個界面向第二個界面 進行傳值 正向傳值 屬性傳值 第二張向第一張界面傳值 反向傳值 下級界面向上一級界面傳值---》反向傳值 反向傳值方式: 1.代理傳值 下級界面要把textField的內容 傳給 上一級,這時下級界面就能夠委託上級界面 修改 label的值 第二個界面(主動方) 能夠制定一個協議 規範代理的行爲, 第一個界面(被動方) 遵照協議 做爲 代理 2.單例傳值 1.系統單例 2.自定義單例 UIApplication 單例傳值 第二個界面 把值給單例 第一個從單例中獲取值 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)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //將要顯示的時候 從單例中獲取 值 UIApplication *app = [UIApplication sharedApplication]; AppDelegate *appDelegate = app.delegate; //修改值 _label.text = appDelegate.labelStr; } - (void)btnClick:(UIButton *)btn { //每次點擊按鈕 都會建立一個新的第二張對象 SecondViewController *svc = [[SecondViewController alloc] init]; [self presentViewController:svc animated:YES completion:nil]; [svc release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在SecondViewController以下代碼代理
#import "SecondViewController.h" #import "MyControl.h" #define kDebugPrint NSLog(@"%s",__func__) #import "AppDelegate.h" @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 { //點擊傳值按鈕先把 數據給 單例 //UIApplication //獲取單例 UIApplication *app = [UIApplication sharedApplication]; //獲得AppDelegate AppDelegate *appDelegate = app.delegate; //傳值 把值 賦給appDelegate的屬性 appDelegate.labelStr = _textField.text; } @end