国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

iOS傳值之代理傳值

2019-11-14 18:38:12
字體:
供稿:網(wǎng)友

iOS中傳值方式有好幾種,分別是:代理傳值,block傳值,屬性傳值,通知傳值,單例傳值,利用userdefault或者文件方式傳值,通常代理傳值和block傳值使用最普遍,本文介紹代理傳值的方式,后續(xù)博客會一次寫上其他傳值方式。

一 什么是委托代理?

1、協(xié)議(PRotocol),就是使用了這個協(xié)議后,必須按照協(xié)議規(guī)定的內(nèi)容來處理事情,協(xié)議中要求的方法必須實現(xiàn)(@optional的方法除外)。

protocol是一種語法,它提供了一個很方便的、實現(xiàn)delegate模式的機會。

定義protocol如下:

  1. @protocol ClassBDelegate<NSObject> 
  2. - (void)methodOne; 
  3. @optional 
  4. - (void)methodTwo:(NSString *)value; 
  5. @end

定義了一個ClassB的協(xié)議,這個協(xié)議中包含兩個方法,其中methodTwo為可選的。

在ClassA的頭文件(ClassA.h)中實現(xiàn)這個協(xié)議,如下代碼:

  1. @interface ClassA<ClassBDelegate> 
  2. @end

在ClassA的實現(xiàn)文件(ClassA.m)中實現(xiàn)ClassBDelegate的兩個方法,其中methodTwo可以不實現(xiàn),如下:

  1. - (void)methodOne{ 
  2.     // 具體實現(xiàn)內(nèi)容 
  3.  
  4. - (void)methodTwo:(NSString *)value{  
  5.     // 具體實現(xiàn)內(nèi)容   
  6. }

2、代理(delegate),顧名思義就是委托別人辦事,當一件事情發(fā)生后,自己不處理,讓別人來處理。

delegate和protocol沒有關系。delegate本身是一種設計模式。是把一個類自己需要做的一部分事情,讓另一個類(也可以就是自己本身)來完成。

在ClassB的頭文件(ClassB.h)中定義一個代理如下:

  1. @interface ClassB 
  2. @property (nonatomic, unsafe_unretained) id<ClassBDelegate> delegate; 
  3. @end

這樣,當我們在ClassB的實現(xiàn)文件(ClassB.m)中遇到想讓別的類(如 ClassA)處理的問題時,就可以這樣

  1. [self.delegate methodOne]; 
  2. [self.delegate methodTwo:@"需要傳遞的值"];

二  具體實例

   實現(xiàn)的功能是:在viewcontroller中創(chuàng)建一個UIButton按鈕用于push到下一個頁面,和一個UILable用于顯示從第二個頁面?zhèn)骰氐奈淖郑趕econdviewcontroller中創(chuàng)建一個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 實現(xiàn)代理方法

- (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>

//聲明協(xié)議方法

- (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{

    //將本頁面獲取的值傳遞給上一個頁面去實現(xiàn)

    [self.delegate getText:_textField.text];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

三  模擬器運行結(jié)果截圖

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 图木舒克市| 黔西县| 宜君县| 涞源县| 沾化县| 陈巴尔虎旗| 大余县| 贵德县| 若尔盖县| 维西| 马关县| 太原市| 于都县| 大姚县| 雷波县| 镇雄县| 嘉兴市| 常宁市| 榆林市| 师宗县| 南部县| 临澧县| 积石山| 丽江市| 宝应县| 武山县| 大姚县| 宝清县| 水富县| 丰宁| 化州市| 运城市| 平邑县| 沙坪坝区| 怀化市| 巴彦淖尔市| 花垣县| 侯马市| 岑巩县| 洛宁县| 克什克腾旗|