標籤:
代理是IOS開發中用到的一種設計模式。今天做了一個代理的小練習:
以下項目實現了兩個頁面之間的相互切換,並且在切換頁面的時候完成了從一個頁面往另一個頁面的傳值。從首頁面往其他頁面傳值是容易的,但是反過來從其他頁面往首頁面傳值就難了,要用到delegate設計模式。
建立一個ios的項目,然後再建立一個ViewController,起名為TwoViewController,這樣,加上建立項目時預設產生的那個ViewController,總共有兩個ViewController,其中建立項目預設產生的那個是主ViewController。
為了使用代理模式,我們要先定義一個協議,這個協議如下:
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIKit.h> 3 //定義一個協議,協議中定義一個傳值的方法 4 //定義一個協議,協議中定義一個傳值的方法 5 //定義一個協議,協議中定義一個傳值的方法 6 @protocol Mydelegate <NSObject> 7 8 @required 9 -(void)changeValue:(NSString *)stringnow;10 11 @end
協議中定義了一個傳值的函數,用來把第二個ViewController中的值傳給第一個ViewController,如果不是用代理,我們只能從第一個ViewController向第二個ViewController傳值,而不能反過來,從第二個ViewController給第一個ViewController傳值(具體原因沒理解,以後理解了再補上)
完成協議後讓第一個ViewController遵守這個協議,也就是在第一個ViewController的.h檔案中聲明第一個ViewController遵守這個協議<Mydelegate>:
1 #import <UIKit/UIKit.h> 2 3 //第一個頁面應用Mydelegate協議 4 //第一個頁面應用Mydelegate協議 5 //第一個頁面應用Mydelegate協議 6 #import "Mydelegate.h" 7 @interface ViewController : UIViewController <Mydelegate> 8 9 10 @end
接下來在第一個ViewController的.m檔案中實現changeValue這個傳值方法:
1 #import "ViewController.h" 2 #import "TwoViewController.h" 3 @interface ViewController (){ 4 UITextField *textFieldnow; 5 6 } 7 8 @end 9 10 11 12 @implementation ViewController13 14 //第一個頁面引用Mydelegate協議,故要在.m檔案中實現Mydelegate中定義的必須要實現的方法15 //第一個頁面引用Mydelegate協議,故要在.m檔案中實現Mydelegate中定義的必須要實現的方法16 //第一個頁面引用Mydelegate協議,故要在.m檔案中實現Mydelegate中定義的必須要實現的方法17 -(void)changeValue:(NSString *)stringnow{18 textFieldnow.text=stringnow;19 }20 21 22 23 - (void)viewDidLoad {24 [super viewDidLoad];25 // 初始化textFieldnow26 textFieldnow = [[UITextField alloc]initWithFrame:CGRectMake(50 , 200, 200, 50)];27 28 // 設定textFieldnow的背景顏色29 textFieldnow.backgroundColor = [UIColor colorWithRed:0.139 green:0.760 blue:1.000 alpha:1.000];30 31 // 為textFieldnow添加輸入完成後讓鍵盤消失的事件32 [textFieldnow addTarget:self action:@selector(losefirstresponsder:) forControlEvents:UIControlEventEditingDidEndOnExit];33 34 // 把textFieldnow添加到view中35 [self.view addSubview:textFieldnow];36 37 38 39 }40 41 42 //本函數的功能是Textfield在輸入完後按return讓虛擬鍵盤消失43 -(void)losefirstresponsder:(id)sender{44 [self resignFirstResponder];45 }46 47 48 - (IBAction)TurnToNext:(id)sender {49 TwoViewController *twoView = [[TwoViewController alloc]init];50 51 // 委託機制最關鍵的部分,在跳轉到第二個介面之前把第二個介面的委派物件指向自己52 // 委託機制最關鍵的部分,在跳轉到第二個介面之前把第二個介面的委派物件指向自己53 // 委託機制最關鍵的部分,在跳轉到第二個介面之前把第二個介面的委派物件指向自己54 twoView.delegate = self;55 56 57 // 把第一個介面的textFieldnow的值傳給第二個介面中的namestring58 twoView.namestring = textFieldnow.text;59 60 61 // 跳轉到第二個介面62 [self presentViewController:twoView animated:YES completion:nil];63 64 }65 66 - (void)didReceiveMemoryWarning {67 [super didReceiveMemoryWarning];68 // Dispose of any resources that can be recreated.69 }70 71 @end
第二個ViewController的.h檔案如下:
1 #import <UIKit/UIKit.h> 2 #import "Mydelegate.h" 3 4 @interface TwoViewController : UIViewController 5 6 @property(nonatomic,retain )NSString *namestring; 7 //定義一個變數delegate 8 //定義一個變數delegate 9 //定義一個變數delegate10 @property(nonatomic,weak) id <Mydelegate> delegate;11 12 @end
第二個ViewController的.m檔案如下:
1 #import "TwoViewController.h" 2 3 @interface TwoViewController (){ 4 UILabel *label; 5 UITextField *textFieldnow1; 6 7 } 8 9 @end10 11 @implementation TwoViewController12 @synthesize namestring;13 @synthesize delegate;14 - (void)viewDidLoad {15 [super viewDidLoad];16 // Do any additional setup after loading the view from its nib.17 label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];18 [self.view addSubview:label];19 label.text = namestring;20 textFieldnow1 = [[UITextField alloc]initWithFrame:CGRectMake(50 , 200, 200, 50)];21 22 textFieldnow1.backgroundColor = [UIColor colorWithRed:0.139 green:0.760 blue:1.000 alpha:1.000];23 [textFieldnow1 addTarget:self action:@selector(losefirstresponsder:) forControlEvents:UIControlEventEditingDidEndOnExit];24 [self.view addSubview:textFieldnow1];25 26 27 }28 - (IBAction)BackToHome:(id)sender {29 [self dismissViewControllerAnimated:YES completion:nil];30 [delegate changeValue:textFieldnow1.text];31 }32 33 -(void)losefirstresponsder:(id)sender{34 [self resignFirstResponder];35 }36 37 - (void)didReceiveMemoryWarning {38 [super didReceiveMemoryWarning];39 // Dispose of any resources that can be recreated.40 }41 @end
要實現通過代理讓第二個頁面傳遞一個值給第一個頁面,最關鍵的一點就是在第一個頁面中要有一個指標指向self,也就是twoView.delegate = self;這條語句:
1 // 委託機制最關鍵的部分,在跳轉到第二個介面之前把第二個介面的委派物件指向自己2 // 委託機制最關鍵的部分,在跳轉到第二個介面之前把第二個介面的委派物件指向自己3 // 委託機制最關鍵的部分,在跳轉到第二個介面之前把第二個介面的委派物件指向自己4 twoView.delegate = self;
而協議的作用就是提供了一個函數,起到了橋樑的作用。
IOS開發-UI學習-delegate(代理)的使用