IOS代理模式

來源:互聯網
上載者:User

標籤:

  代理困擾了很久,一直看概念,看網上的部落格發現不是自己要的!我想要的是一個代理流程。

  第一個介面的名字,姑且我們叫apple吧!第二個介面的名字叫banana;在banana中的值想傳給apple,這時候就要用到代理了,可以用其他的傳值嗎?可以,不過我們先研究代理傳值。

  banana傳值,banana就要找個代理。

  一.banana

  第一步:

      在banana.h中定義代理

  @protocol BananaDelegate <NSObject>

  // 聲明一個代理方法:預設是@require,必須實現代理方法;@optional,非必須實現。

  - (void)bananaDelegate:(BananaViewController *)controller bananaModel:(BananaModel *)model;

  @end

  @interface BananaViewController : UIViewController

  @property (nonatomic, strong) id<BananaDelegate> delegate;

  @end

  第二步:

  在banana.m中返回apple中需要的代碼  

  - (IBAction)BackButtonClicked:(id)sender

  {

      BananaModel *m = [[BananaModel alloc] init];

      // 給m賦值吧!假如:

    m.name = @"張三";

    // 判斷是apple中否實現代理方法 

      if([self.delegate respondsToSelector:@selector(bananaDelegate:bananaModel:)])

      {

          [self.delegate bananaDelegate:self bananaModel:m];

      }

      [self.navigationController popViewControllerAnimated:YES];

  }

  二.apple 

  第一步:

  @interface AppleViewController ()<BananaDelegate>

  @end

  第二步:

  設定代理

  controller.delegate = self;

  第三步:

  實現代理方法

  - (void)bananaDelegate:(BananaViewController *)controller bananaModel:(BananaModel *)model

  {

    //  這是得到傳來的值,你完成你想要的任務吧!
  }

  只是看沒有啥用,多寫幾遍直至熟練!然後,就能領會到了!

IOS代理模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.