IOS開發-Protocol協議及委託代理(Delegate)傳值

來源:互聯網
上載者:User

IOS開發-Protocol協議及委託代理(Delegate)傳值
前言:因為Object-C是不支援多繼承的,所以很多時候都是用Protocol(協議)來代替。Protocol(協議)只能定義公用的一套介面,但不能提供具體的實現方法。也就是說,它只告訴你要做什麼,但具體怎麼做,它不關心。   當一個類要使用某一個Protocol(協議)時,都必須要遵守協議。比如有些必要實現的方法,你沒有去實現,那麼編譯器就會警示告,來提醒你沒有遵守××協議。注意,我這裡說的是警告,而不是錯誤。對的,就算你不實現那些“必要實現”的方法,程式也是能啟動並執行,只不過多了些警告。   我會在本文的結尾放上此Demo的,有需要的話可以去下載,謝謝。   Protocol(協議)的作用: 一、定義一套公用的介面(Public)   @required:必須實現的方法   @optional:可選實現的方法(可以全部都不實現) 二、委託代理(Delegate)傳值:   它本身是一個設計模式,它的意思是委託別人去做某事。   比如:兩個類之間的傳值,類A調用類B的方法,類B在執行過程中遇到問題通知類A,這時候我們需要用到代理(Delegate)。   又比如:控制器(Controller)與控制器(Controller)之間的傳值,從C1跳轉到C2,再從C2返回到C1時需要通知C1更新UI或者是做其它的事情,這時候我們就用到了代理(Delegate)傳值。   一、定義一套公用的介面(Public)      ProtocolDelegate.h代碼(協議不會產生.m檔案):  #import <Foundation/Foundation.h> @protocol ProtocolDelegate <NSObject> // 必須實現的方法@required- (void)error; // 可選實現的方法@optional- (void)other;- (void)other2;- (void)other3; @end     在需要使用到協議的類,import它的標頭檔: #import "ViewController.h"#import "ProtocolDelegate.h"  我這裡選擇的是入口檔案   記得要遵守協議: @interface ViewController () <ProtocolDelegate> @end    這時會報一個警告,因為定義的協議裡有一個是必須實現的方法,而我們沒有去實現:       實現了必須實現的方法後,編譯器就不會警示告了:   至於其它的可選方法,你可以選擇實現,也可以全都不實現。   二、委託代理(Delegate)傳值          下面放出主要類檔案代碼,我在裡面寫了注釋,大家應該能看懂。不懂也沒有關係,我會在本文結尾放上Demo。 ViewController.m檔案:  #import "ViewController.h"#import "ProtocolDelegate.h"#import "ViewControllerB.h" @interface ViewController () <ProtocolDelegate, ViewControllerBDelegate> @end @implementation ViewController - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    ViewControllerB *vc = segue.destinationViewController;    [vc setDelegate:self];} // 這裡實現B控制器的協議方法- (void)sendValue:(NSString *)value{    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:value delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];    [alertView show];} - (void)error{    }  @end     ViewControllerB.h檔案:  #import <UIKit/UIKit.h> // 建立一個協議,協議的名字一般是由“類名+Delegate”@protocol ViewControllerBDelegate <NSObject> // 代理傳值方法- (void)sendValue:(NSString *)value; @end @interface ViewControllerB : UIViewController // 委託代理人,代理一般需使用弱引用(weak)@property (weak, nonatomic) id<ViewControllerBDelegate> delegate; @end    ViewControllerB.m檔案:  #import "ViewControllerB.h" @interface ViewControllerB () @property (strong, nonatomic) IBOutlet UITextField *textField; @end @implementation ViewControllerB - (IBAction)backAction:(id)sender{    if ([_delegate respondsToSelector:@selector(sendValue:)]) { // 如果協議響應了sendValue:方法        [_delegate sendValue:_textField.text]; // 通知執行協議方法    }    [self.navigationController popViewControllerAnimated:YES];} @end        小結: 當你需要定義一套公用的介面,實現方法可以是不同的時候,你可以使用Protocol協議。 當你需要進行類與類之間的傳值時,你也可以基於Protocol協議,使用代理設計模式進行傳值。   

相關文章

聯繫我們

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