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。

  • 相关阅读:
    第六周作业:《人月神话》对我做项目实践的启示(一)
    第五周作业:网站的初步设计
    关于做团队项目时需求分析工作中所学的一部分知识
    软件工程学生的编程能力与编程语言是中文或英文有关系吗?
    面向过程(或者叫结构化)分析方法与面向对象分析方法到底区别在哪里?请根据自己的理解简明扼要的回答。
    当下大部分互联网创业公司为什么都愿意采用增量模型来做开发
    1+X Web前端开发(中级)理论考试样题(附答案)
    1+X Web前端开发(初级)理论考试样题(附答案)
    vi 和vim 的区别
    Linux查看日志三种命令
  • 原文地址:https://www.cnblogs.com/dj3839/p/6732685.html
Copyright © 2011-2022 走看看