• 1225.1——代理模式


    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

  • 相关阅读:
    回顾2016,工作总结!
    上传base64格式的图片到服务器
    input输入提示历史记录
    input输入时软键盘回车显示搜索
    JS设置和读取Cookie
    正则表达式识别字符串中的URL
    X-Frame-Options配置
    pytest学习笔记
    测试理论基础总结
    redis杂七杂八
  • 原文地址:https://www.cnblogs.com/damonWq/p/5077074.html
Copyright © 2020-2023  润新知