zoukankan      html  css  js  c++  java
  • 协议和代理的理解及使用

    前言:因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替。Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法。也就是说,它只告诉你要做什么,但具体怎么做,它不关心。

    当一个类要使用某一个Protocol(协议)时,都必须要遵守协议。比如有些必要实现的方法,你没有去实现,那么编译器就会报警告,来提醒你没有遵守××协议。注意,我这里说的是警告,而不是错误。对的,就算你不实现那些“必要实现”的方法,程序也是能运行的,只不过多了些警告。

    Protocol(协议)的作用:
    一、定义一套公用的接口(Public)
     
      @required:必须实现的方法
     
      @optional:可选实现的方法(可以全部都不实现)
     
    二、委托代理(Delegate)传值:
     
      它本身是一个设计模式,它的意思是委托别人去做某事。
     
      比如:两个类之间的传值,类A调用类B的方法,类B在执行过程中遇到问题通知类A,这时候我们需要用到代理(Delegate)。
     
      又比如:控制器(Controller)与控制器(Controller)之间的传值,从C1跳转到C2,再从C2返回到C1时需要通知C1更新UI或者是做其它的事情,这时候我们就用到了代理(Delegate)传值。
     
     
    一、定义一套公用的接口(Public)
     
    ProtocolDelegate.h代码(协议不会生成.m文件):
     
    #import <Foundation/Foundation.h>
     
    @protocol ProtocolDelegate <NSObject>
     
    // 必须实现的方法
    @required
    - (void)error;
     
    // 可选实现的方法
    @optional
    - (void)other;
    - (void)other2;
    - (void)other3;
     
    在需要使用到协议的类,import它的头文件:
     
    #import "ViewController.h"
    #import "ProtocolDelegate.h"
     
     
    我这里选择的是入口文件
     
     
     
    记得要遵守协议:
     
    @interface ViewController () <ProtocolDelegate>
     
    @end
    这时会报一个警告,因为定义的协议里有一个是必须实现的方法,而我们没有去实现:
     
    实现了必须实现的方法后,编译器就不会报警告了:
     
    至于其它的可选方法,你可以选择实现,也可以全都不实现。
     
    二、委托代理(Delegate)传值
     
    下面放出主要类文件代码,我在里面写了注释,大家应该能看懂。不懂也没有关系,我会在本文结尾放上Demo下载地址。
     
    ViewController.m文件:
     
     
    #import "ViewController.h"
    #import "ProtocolDelegate.h"
    #import "ViewControllerB.h"
     
    @interface ViewController () <ProtocolDelegate, ViewControllerBDelegate>
     
    @end
    @implementation ViewController
     
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        ViewControllerB *vc = segue.destinationViewController;
        [vc setDelegate:self];
    }
     
    // 这里实现B控制器的协议方法
    - (void)sendValue:(NSString *)value
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:value delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    - (void)error
    {
        
    }
     
     
    @end
     
     
    ViewControllerB.h文件:
     
    #import <UIKit/UIKit.h>
     
    // 新建一个协议,协议的名字一般是由“类名+Delegate”
    @protocol ViewControllerBDelegate <NSObject>
     
    // 代理传值方法
    - (void)sendValue:(NSString *)value;
     
    @end
     
    @interface ViewControllerB : UIViewController
     
    // 委托代理人,代理一般需使用弱引用(weak)
    @property (weak, nonatomic) id<ViewControllerBDelegate> delegate;
     
    @end
     
     
     
    ViewControllerB.m文件:
    #import "ViewControllerB.h"
     
    @interface ViewControllerB ()
     
    @property (strong, nonatomic) IBOutlet UITextField *textField;
     
    @end
     
    @implementation ViewControllerB
     
    - (IBAction)backAction:(id)sender
    {
        if ([_delegate respondsToSelector:@selector(sendValue:)]) { // 如果协议响应了sendValue:方法
            [_delegate sendValue:_textField.text]; // 通知执行协议方法
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
     
    @end
    小结:
     
    当你需要定义一套公用的接口,实现方法可以是不同的时候,你可以使用Protocol协议。
     
    当你需要进行类与类之间的传值时,你也可以基于Protocol协议,使用代理设计模式进行传值。 
     
  • 相关阅读:
    SAP中的文档维护
    SAP日期处理函数汇总(转)
    PR PO通过fm创建时,如何传输增强字段
    html+jQuery简单的利息计算器
    上传项目到github
    Nodejs-毕业设计5-小技巧
    Nodejs-毕业设计4-登录页面
    Nodejs-毕业设计3-路径
    Nodejs-毕业设计2-下载依赖准备工作
    Nodejs-毕业设计1-部署环境
  • 原文地址:https://www.cnblogs.com/fs-ios/p/4777770.html
Copyright © 2011-2022 走看看