zoukankan      html  css  js  c++  java
  • oc中的委托模式

    通过一个例子来理解委托模式

    首先定义个协议

    协议(protocol) :它可以声明一些必须实现的方法和选择实现的方法  (在java中称为接口)

    //
    //  StudentDelegate.h
    //  test
    //
    //  Created by 洪东楗 on 2017/4/19.
    //  Copyright © 2017年 洪东楗. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol StudentDelegate <NSObject>
    
    @optional
    - (void)write;
    @end

    这个协议表示,你可以选择性的实现write这个方法

    我们接着定义一个通用类

    通用类:就好比NSTexiField,你想要调用的

    //
    //  Student.h
    //  test
    //
    //  Created by 洪东楗 on 2017/4/19.
    //  Copyright © 2017年 洪东楗. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "StudentDelegate.h"
    @interface Student : NSObject
    @property (assign) id<StudentDelegate> delegate;
    - (void)read;
    @end
    //
    //  Student.m
    //  test
    //
    //  Created by 洪东楗 on 2017/4/19.
    //  Copyright © 2017年 洪东楗. All rights reserved.
    //
    
    #import "Student.h"
    
    @implementation Student
    - (void)read {
        if ([_delegate respondsToSelector:@selector(write)]) {
            [_deleage write];
            NSLog(@"read success");
        } else {
            NSLog(@"read failed");
        }
    }
    @end

    这Student.h这个文件中 ,导入了刚刚所定义的协议StudentDelegate.h,并声明了一个delegate属性,这个属性是id类型的,id就是一个可以指向任何对象的指针,在这里指明了是StudentDelegate这个协议,并声明了read这个方法。

    在Student.m这个文件中,实现了read方法。并在方法中表示,如果delegate这个对象,实现了write这个方法,那么就调用write方法,并输出"read success",否则输出"read failed"。

    接着我们开始使用这个通用类

    //
    //  ViewController.m
    //  test
    //
    //  Created by 洪东楗 on 2017/4/19.
    //  Copyright © 2017年 洪东楗. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Student.h"
    
    @interface ViewController () <StudentDelegate>@end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        Student *s = [[Student alloc] init];
        s.deleage = self;
        [s read];
        //s.num = 10;
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)write{
        NSLog(@"success");
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end

    在这里,通过实现StudentDelegate这个协议,让read方法去调用这个wirte

    这就是一个委托,我调用Student对象中的read, Student对象就会自己判断是否有write,有则先调用write,否则输出read failed。

    接下来是我个人的推断:

    这就好比NSTextField,如果你实现了NSTextFieldDelegate这个协议中的textFieldDidBeginEditing这个方法,NSTextField对象就会在你点击textfield,准备输入时,自动的调用textFieldDidBeginEditing。

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        NSLog(@"begin");
    }

    如这个,它就会自己在控制台输出begin。

  • 相关阅读:
    利用“通过'反射方式'创建的对象”来创建对象的性能问题?
    Linq 学习笔记(一)
    说说数据类型转换帮助类
    使用公共静态属性的方式来代替公共静态字段(C#)
    支持差异数据保存的数据库实体类设计(二)(续)
    编程杂记
    Linq 学习笔记(二)
    UI版本WinDBG常用命令
    Oracle数据库卸载
    SAS框架问世
  • 原文地址:https://www.cnblogs.com/dj3839/p/6732685.html
Copyright © 2011-2022 走看看