• 12月25号 代理模式


    代理模式

     

    1.一般把协议放在代理中(见Person.h)

    2.接收的类型用id 并且服从协议(@property(nonatomic,assign)id <agentDelegate> delegate)

    3.一定要确保对象实现了方法([self.delegate respondsToSelector:@selector(call:)])

    Person.h

    #import <Foundation/Foundation.h>

    #import "Agent.h"

    @interface Person : NSObject <agentDelegate> 

    -(void)needHouse;

    -(void)call:(NSString *)message;

    @end

    Person.m 

    #import "Person.h"

    #import "Agent.h" 

    @implementation Person

    -(void)needHouse{

        Agent * ag = [[Agent alloc]init];

        ag.delegate = self;

        [ag rentHouse];   

    }

    -(void)call:(NSString *)message{

        NSLog(@"%@",message);

    } 

    @end

    Agent.h

    @protocol agentDelegate <NSObject>

    @optional

    -(void)call:(NSString*)message;

    @end

     

    @interface Agent : NSObject

    @property(nonatomic,assign)id <agentDelegate> delegate;

    -(void)rentHouse;

    @end

    Agent.m 

    @implementation Agent

    -(void)rentHouse{

        NSLog(@"正在租房子");

        if ([self.delegate respondsToSelector:@selector(call:)]){

            [self.delegate call:@"租到房子了"];

        } 

    }

    @end

  • 相关阅读:
    html5之缩放图标
    html5之图片的缩放scale
    html5之打地鼠100%胜率
    html5之调整旋转中心点
    html5之三角旋转
    html5中模块居中
    html5中2d图片旋转
    html5之动态移动图
    html5之steps
    读微信开放文档未解记录
  • 原文地址:https://www.cnblogs.com/hmzxwky/p/5081494.html
Copyright © 2020-2023  润新知