iOS傳值之代理傳值,ios傳值代理
iOS中傳值方式有好幾種,分別是:代理傳值,block傳值,屬性傳值,通知傳值,單例傳值,利用userdefault或者檔案方式傳值,通常代理傳值和block傳值使用最普遍,本文介紹代理傳值的方式,後續部落格會一次寫上其他傳值方式。
一 什麼是委託代理?
1、協議(protocol),就是使用了這個協議後,必須按照協議規定的內容來處理事情,協議中要求的方法必須實現(@optional的方法除外)。
protocol是一種文法,它提供了一個很方便的、實現delegate模式的機會。
定義protocol如下:
- @protocol ClassBDelegate<NSObject>
- - (void)methodOne;
- @optional
- - (void)methodTwo:(NSString *)value;
- @end
定義了一個ClassB的協議,這個協議中包含兩個方法,其中methodTwo為可選的。
在ClassA的標頭檔(ClassA.h)中實現這個協議,如下代碼:
- @interface ClassA<ClassBDelegate>
- @end
在ClassA的實現檔案(ClassA.m)中實現ClassBDelegate的兩個方法,其中methodTwo可以不實現,如下:
- - (void)methodOne{
- // 具體實現內容
- }
-
- - (void)methodTwo:(NSString *)value{
- // 具體實現內容
- }
2、代理(delegate),顧名思義就是委託別人辦事,當一件事情發生後,自己不處理,讓別人來處理。
delegate和protocol沒有關係。delegate本身是一種設計模式。是把一個類自己需要做的一部分事情,讓另一個類(也可以就是自己本身)來完成。
在ClassB的標頭檔(ClassB.h)中定義一個代理如下:
- @interface ClassB
- @property (nonatomic, unsafe_unretained) id<ClassBDelegate> delegate;
- @end
這樣,當我們在ClassB的實現檔案(ClassB.m)中遇到想讓別的類(如 ClassA)處理的問題時,就可以這樣
- [self.delegate methodOne];
- [self.delegate methodTwo:@"需要傳遞的值"];
二 具體執行個體
實現的功能是:在viewcontroller中建立一個UIButton按鈕用於push到下一個頁面,和一個UILable用於顯示從第二個頁面傳回的文字,在secondviewcontroller中建立一個UITextfield用於輸入文字。在輸入完成按下back返回第一個頁面後,在lable上顯示輸入的文字。
1 工程
2 ViewController.h檔案
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
3 ViewController.m檔案
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<getTextFromDelegate>
{
UIButton *_button;
UILabel *_lable;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initLayout];
}
- (void)initLayout{
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(0, 0, 100, 50);
_button.center = self.view.center;
_button.backgroundColor = [UIColor redColor];
[_button addTarget:self action:@selector(pushToNextViewController:) forControlEvents:UIControlEventTouchUpInside];
[_button setTitle:@"下一頁" forState:UIControlStateNormal];
[self.view addSubview:_button];
_lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 64, 355, 200)];
_lable.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_lable];
}
- (void)pushToNextViewController:(UIButton *)sender{
SecondViewController *secondVC = [SecondViewController new];
//代理就是本身
secondVC.delegate = self;
[self.navigationController pushViewController:secondVC animated:YES];
}
#pragma mark 實現代理方法
- (void)getText:(NSString *)text{
_lable.text = text;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4 SecondViewController.h檔案
#import <UIKit/UIKit.h>
@protocol getTextFromDelegate <NSObject>
//聲明協議方法
- (void)getText:(NSString *)text;
@end
@interface SecondViewController : UIViewController
@property (weak, nonatomic)id<getTextFromDelegate>delegate;
@end
4 SecondViewController.m檔案
#import "SecondViewController.h"
@interface SecondViewController ()<getTextFromDelegate>
{
UITextField *_textField;
}
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
_textField.backgroundColor = [UIColor redColor];
_textField.center = self.view.center;
[self.view addSubview:_textField];
}
#pragma mark 在頁面將要消失時
- (void)viewWillDisappear:(BOOL)animated{
//將本頁面擷取的值傳遞給上一個頁面去實現
[self.delegate getText:_textField.text];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
三 模擬器運行結果