iOS中傳值方式有好幾種,分別是:代理傳值,block傳值,屬性傳值,通知傳值,單例傳值,利用userdefault或者文件方式傳值,通常代理傳值和block傳值使用最普遍,本文介紹代理傳值的方式,后續博客會一次寫上其他傳值方式。
一 什么是委托代理?
1、協議(PRotocol),就是使用了這個協議后,必須按照協議規定的內容來處理事情,協議中要求的方法必須實現(@optional的方法除外)。
protocol是一種語法,它提供了一個很方便的、實現delegate模式的機會。
定義protocol如下:
定義了一個ClassB的協議,這個協議中包含兩個方法,其中methodTwo為可選的。
在ClassA的頭文件(ClassA.h)中實現這個協議,如下代碼:
在ClassA的實現文件(ClassA.m)中實現ClassBDelegate的兩個方法,其中methodTwo可以不實現,如下:
2、代理(delegate),顧名思義就是委托別人辦事,當一件事情發生后,自己不處理,讓別人來處理。
delegate和protocol沒有關系。delegate本身是一種設計模式。是把一個類自己需要做的一部分事情,讓另一個類(也可以就是自己本身)來完成。
在ClassB的頭文件(ClassB.h)中定義一個代理如下:
這樣,當我們在ClassB的實現文件(ClassB.m)中遇到想讓別的類(如 ClassA)處理的問題時,就可以這樣
二 具體實例
實現的功能是:在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
三 模擬器運行結果截圖
新聞熱點
疑難解答