iOS開發————對象之間通訊之代理協議

來源:互聯網
上載者:User

標籤:

 一、代理實現對象間通訊:

 確定誰是代理對象,誰是被代理對象。

 事件在哪裡發生?誰就是被代理對象

 事件在哪裡響應?誰就是代理對象

 

需求:

舉例:保姆照顧小孩,當小孩髒了時給她清潔,當小孩不開心時陪玩。

被代理類 Child

 定義代理屬性(id),並且此代理屬性要遵守協議

 事件發生時,要調用代理屬性的協議方法來響應這個事件。

 

代理類 Nanny

 要遵守協議

 實現協議方法,協議方法中的內容就是對事件做出的響應。


代理設計模式解決是程式架構上的問題,使程式架構更合理,更具擴充性,降低類與類之前的耦合性。


 需要的對象:

 代理對象:Nanny, Grandma

 被代理對象:Child

 

 流程:

 1.定義一個協議,協議中聲明一些方法。

 2.定義代理類遵守協議,它就包含了協議所有方法的聲明。

 3.被代理類中應該定義一個代理對象,設定類型為通用類型id<協議名> delegate;

 4.當被代理對象要完成某此它無法完成的功能時,它通知代理對象來協助他完成,即調用代理類中實現的協議方法來完成。

 

首先設定協議類

#import <Foundation/Foundation.h>@class Child;//照顧小孩的協議,必須遵守這個協議才有照顧小孩的資格@protocol CareBabyProtocol <NSObject>//給小孩洗澡- (void)batheChild:(Child *)child;//逗小孩開心- (void)playWithChild:(Child *)child;@end

在小孩類中定義代理對象

#import "CareBabyProtocol.h"#import <Foundation/Foundation.h>@interface Child : NSObject@property NSInteger cleanValue; //清潔度@property NSInteger happyValue; //快樂度//被代理類中應該定義一個代理對象,設定類型為通用類型id<協議名> delegate;//小孩中擁有一個通用型(id)的代理對象,並讓此對象遵守協議@property id<CareBabyProtocol> delegate;- (instancetype)initWithCleanValue:(NSInteger)cleanValue happyValue:(NSInteger)happyValue;@end

在小孩不乾淨或者不高興的時候實現代理中的方法

#import "Child.h"@implementation Child- (instancetype)initWithCleanValue:(NSInteger)cleanValue happyValue:(NSInteger)happyValue {        self = [super init];        if (self) {                NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];                _cleanValue = cleanValue;                _happyValue = happyValue;                    }        return self;}- (void)timerAction:(NSTimer *)timer {        _cleanValue--;    _happyValue--;        NSLog(@"clean:%li, happy:%li", _cleanValue, _happyValue);        //當小孩的清潔度下降某個限定值時,通知保姆來給他洗澡    if (_cleanValue == 95) {                [self.delegate batheChild:self];                    }        if (_happyValue == 90) {        [self.delegate playWithChild:self];    }    }@end

定義代理對象保姆,並讓保姆簽署協議

#import "CareBabyProtocol.h"#import <Foundation/Foundation.h>@class Child;@interface Nanny : NSObject <CareBabyProtocol>@end

保姆的協議方法實現

#import "Nanny.h"#import "Child.h"@implementation Nanny- (void)batheChild:(Child *)child {        NSLog(@"小孩髒了,保姆把小孩洗白白");    child.cleanValue = 100;    }- (void)playWithChild:(Child *)child {        NSLog(@"小孩鬧人了,保姆逗他開心");    child.happyValue = 100;    }@end

在main中開啟小孩對象和代理對象(保姆)

#import "Child.h"#import "Nanny.h"#import "Grandma.h"#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {                        //Nanny *nanny = [[Nanny alloc] init];        Grandma *grandma = [[Grandma alloc] init];                Child *child = [[Child alloc] initWithCleanValue:100 happyValue:100];                //將保姆作為小孩的代理對象        //child.nanny = nanny;                //將奶奶作為小孩的代理對象        child.delegate = grandma;                        [[NSRunLoop currentRunLoop] run];                            }    return 0;}



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.