iOS傳值之代理傳值,ios傳值代理

來源:互聯網
上載者:User

iOS傳值之代理傳值,ios傳值代理

iOS中傳值方式有好幾種,分別是:代理傳值,block傳值,屬性傳值,通知傳值,單例傳值,利用userdefault或者檔案方式傳值,通常代理傳值和block傳值使用最普遍,本文介紹代理傳值的方式,後續部落格會一次寫上其他傳值方式。

一 什麼是委託代理?

1、協議(protocol),就是使用了這個協議後,必須按照協議規定的內容來處理事情,協議中要求的方法必須實現(@optional的方法除外)。

protocol是一種文法,它提供了一個很方便的、實現delegate模式的機會。

定義protocol如下:

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

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

在ClassA的標頭檔(ClassA.h)中實現這個協議,如下代碼:

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

在ClassA的實現檔案(ClassA.m)中實現ClassBDelegate的兩個方法,其中methodTwo可以不實現,如下:

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

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

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

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

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

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

  1. [self.delegate methodOne]; 
  2. [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

三  模擬器運行結果

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.