页面传值:属性,协议,Block传值
生活随笔
收集整理的這篇文章主要介紹了
页面传值:属性,协议,Block传值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.屬性傳值和協議傳值
1 #import "RootViewController.h" 2 #import "SecondViewController.h" 3 #warning 第四步:簽訂協議 4 @interface RootViewController ()<SecondViewControllerDelegate> 5 @property(nonatomic,strong)UILabel *showTextLable; 6 @end 7 8 @implementation RootViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view. 13 self.view.backgroundColor = [UIColor whiteColor]; 14 self.navigationItem.title = @"首頁"; 15 //創建UILable用于顯示內容 16 UILabel *showTextLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; 17 showTextLable.backgroundColor = [UIColor yellowColor]; 18 showTextLable.text = @"美女帥哥"; 19 showTextLable.textAlignment = NSTextAlignmentCenter; 20 [self.view addSubview:showTextLable]; 21 self.showTextLable = showTextLable; 22 23 //創建推出下個界面的UIButton 24 UIButton *pushNextBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 25 pushNextBtn.frame = CGRectMake(100, 300, 150, 50); 26 pushNextBtn.backgroundColor = [UIColor magentaColor]; 27 [pushNextBtn setTitle:@"下一頁" forState:UIControlStateNormal]; 28 [pushNextBtn addTarget:self action:@selector(pushNextController) forControlEvents:UIControlEventTouchUpInside]; 29 [self.view addSubview:pushNextBtn]; 30 31 32 } 33 -(void)pushNextController 34 { 35 SecondViewController *secondVC = [[SecondViewController alloc] init]; 36 //需要把lable的text傳給 37 //下面這種傳值方法是錯誤的,因為textField還沒有創建,還沒有加載到視圖上,這里是項目期特別容易犯錯的地方,必須要考慮視圖加載的順序 38 //secondVC.textField.text = self.showTextLable.text; 39 //正確的寫法應該是直接傳送文本內容 40 secondVC.content = self.showTextLable.text; 41 // 42 #warning 第五步:指定代理人 43 secondVC.secondViewControllerDelegate = self;//指定協議代理人 44 [self.navigationController pushViewController:secondVC animated:YES]; 45 } 46 #warning 第六步:實現協議的方法 47 -(void)changeValue:(NSString *)text 48 { 49 self.showTextLable.text = text; 50 } 51 - (void)didReceiveMemoryWarning { 52 [super didReceiveMemoryWarning]; 53 // Dispose of any resources that can be recreated. 54 } 1 #import <UIKit/UIKit.h> 2 #warning 第一步:聲明協議 3 @protocol SecondViewControllerDelegate <NSObject> 4 5 -(void)changeValue:(NSString *)text; 6 @optional//可選的協議方法 7 -(void)changeColor:(UIColor *)color; 8 9 @end 10 @interface SecondViewController : UIViewController 11 /** 12 * 顯示文本輸入框的 13 */ 14 @property (nonatomic,strong) UITextField *textField; 15 /** 16 * 顯示文本輸入框的內容文本 17 */ 18 @property (nonatomic,strong) NSString *content; 19 #warning 第二步:聲明代理人 20 /** 21 * 第二個Controller中的協議, 22 assign避免循環引用 23 */ 24 @property (nonatomic,assign)id<SecondViewControllerDelegate>secondViewControllerDelegate; 25 @end 1 #import "SecondViewController.h" 2 3 @interface SecondViewController () 4 5 @end 6 7 @implementation SecondViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view. 12 self.view.backgroundColor = [UIColor whiteColor]; 13 self.navigationItem.title= @"第二頁"; 14 //創建輸入文本的文本框 15 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; 16 textField.placeholder = @"請輸入文本內容"; 17 textField.borderStyle = UITextBorderStyleRoundedRect; 18 [self.view addSubview:textField]; 19 self.textField = textField; 20 self.textField.text = self.content; 21 22 //創建UIButton用于返回上一個界面 23 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 24 backBtn.frame = CGRectMake(100, 300, 150, 50); 25 [backBtn setTitle:@"返回" forState:UIControlStateNormal]; 26 backBtn.backgroundColor = [UIColor blackColor]; 27 [backBtn addTarget:self action:@selector(popButtonAction) forControlEvents:UIControlEventTouchUpInside]; 28 [self.view addSubview:backBtn]; 29 30 31 } 32 -(void)popButtonAction 33 { 34 #warning 第三步:執行相關的協議方法 35 [self.secondViewControllerDelegate changeValue:self.textField.text]; 36 [self.navigationController popViewControllerAnimated:YES]; 37 } 38 //總結:屬性傳值:一般使用從前往后進行傳值的情況 39 //如果從后往前,需要借助其他傳值方式 40 //協議傳值方式 41 /** 42 * 實現協議的六個步驟: 43 第一步:聲明協議 44 第二步:聲明代理人 45 第三步:執行協議方法 46 第四步:簽訂協議 47 第五步:指定代理人 48 第六步:實現協議方法 49 */2.Block傳值
1 #import "RootViewController.h" 2 #import "SecondViewController.h" 3 @interface RootViewController () 4 @property(nonatomic,strong)UILabel *showTextLable; 5 @end 6 7 @implementation RootViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view. 12 UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; 13 showLabel.backgroundColor = [UIColor redColor]; 14 showLabel.text = @"美女帥哥"; 15 showLabel.textAlignment = NSTextAlignmentCenter; 16 showLabel.font = [UIFont systemFontOfSize:24]; 17 [self.view addSubview:showLabel]; 18 self.showTextLable = showLabel; 19 UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 20 nextBtn.frame = CGRectMake(100, 300, 150, 50); 21 nextBtn.backgroundColor = [UIColor yellowColor]; 22 [nextBtn setTitle:@"下一頁" forState:UIControlStateNormal]; 23 [nextBtn addTarget:self action:@selector(showNextAction) forControlEvents:UIControlEventTouchUpInside]; 24 [self.view addSubview:nextBtn]; 25 26 } 27 -(void)showNextAction 28 { 29 SecondViewController *secondView = [[SecondViewController alloc] init]; 30 [self.navigationController pushViewController:secondView animated:YES]; 31 //ARC模式下用__weak修飾可解決內存泄漏問題,MRC模式下用__block 32 __weak typeof (self)weakSelf = self; 33 #warning 第三步:實現Block的內容(實現傳遞過來的內容) 34 secondView.changeValueBlock = ^(NSString *string){ 35 //下面這種引用容易造成內存泄漏 36 //self.showTextLable.text = string; 37 weakSelf.showTextLable.text = string; 38 }; 39 //block的核心是:回調,Block作為屬性進行傳值的時候,一般使用場景是從第二個界面返回到上一個界面,基本步驟: 40 //1.在第二個界面中聲明一個Block屬性 41 //2.在第二個界面返回的那句代碼之前加上Block需要傳遞的內容 42 //3.在一個界面推出第二個界面那句代碼之前,對Block進行實現(即接受Block傳遞過來的內容) 43 secondView.content = self.showTextLable.text; 44 } 1 #import <UIKit/UIKit.h> 2 3 @interface SecondViewController : UIViewController 4 @property (nonatomic,strong) NSString *content; 5 @property (strong,nonatomic) UITextField *textField; 6 #warning 第一步:聲明Block屬性,copy是為了解決循環引用的問題 7 @property (nonatomic,copy) void(^changeValueBlock)(NSString *string); 8 @end 1 #import "SecondViewController.h" 2 3 @interface SecondViewController () 4 5 @end 6 7 @implementation SecondViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view. 12 self.view.backgroundColor = [UIColor whiteColor]; 13 self.view.backgroundColor = [UIColor whiteColor]; 14 self.navigationItem.title= @"第二頁"; 15 //創建輸入文本的文本框 16 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; 17 textField.placeholder = @"請輸入文本內容"; 18 textField.borderStyle = UITextBorderStyleRoundedRect; 19 [self.view addSubview:textField]; 20 self.textField = textField; 21 self.textField.text = self.content; 22 23 //創建UIButton用于返回上一個界面 24 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 25 backBtn.frame = CGRectMake(100, 300, 150, 50); 26 [backBtn setTitle:@"返回" forState:UIControlStateNormal]; 27 backBtn.backgroundColor = [UIColor blackColor]; 28 [backBtn addTarget:self action:@selector(popButtonAction) forControlEvents:UIControlEventTouchUpInside]; 29 [self.view addSubview:backBtn]; 30 } 31 -(void)popButtonAction 32 { 33 #warning 第二步:將要傳遞的內容通過屬性block先給它 34 self.changeValueBlock(self.textField.text); 35 36 [self.navigationController popViewControllerAnimated:YES]; 37 }?
轉載于:https://www.cnblogs.com/DevinSMR/p/5218153.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的页面传值:属性,协议,Block传值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lintcode-【简单题】快乐数
- 下一篇: 循环语句练习题2(打印三角形和菱形)