一、代理

1.代理的介绍

  代理是一种通用的设计模式  

   代理使用方式:A 让 B 做件事,空口无凭,签个协议。

   所以代理有三部分组成:

       委托方: 定义协议

       协议   : 用来规定代理方可以做什么,必须做什么

       代理方: 按照协议完成委托方的需求

2. 协议的介绍

  协议是定义了一套公用的接口,是方法的列表,但是无法实现。

  可以通过代理,实现协议中的方法。

  协议是公用方法,一般写在一个类里面。

  如果多个类都使用这个协议,可以写成一个peotocol文件。

3.代理的使用

  (1)委托某人做某事

     先建立一个Custom类,Custom类要买东西,就定下一个协议,实现协议的人,就实现custom协议里的方法。

     Custom.h

#import <Foundation/Foundation.h>
@class Customer; @protocol CustomerDelegate <NSObject> @required -(void)custom:(Customer*)Customer buyThingNum:(int)count; @end @interface Customer : NSObject @property(nonatomic,weak)id<CustomerDelegate>delegate; -(void)buyThingNum:(int)count; @end

    Custom.m

#import "Customer.h"

@implementation Customer

-(void)buyThingNum:(int)count
{
if (self.delegate && [self.delegate respondsToSelector:@selector(custom:buyThingNum:)]) {
[self.delegate custom:self buyThingNum:];
}
} @end

viewController.m

#import "ViewController.h"
#import "Customer.h"
@interface ViewController ()<CustomerDelegate> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
Customer *custom = [[Customer alloc]init];
custom.delegate = self;
[custom buyThingNum:];
} -(void)custom:(Customer*)Customer buyThingNum:(int)count
{
NSLog(@"%d",count);
}
@end

(2)反向传值

     定义一个CustomViewController的类,在CustomViewController.h中定义协议。

#import <UIKit/UIKit.h>

@protocol PassValueDelegate <NSObject>

@required

-(void)buyThingNum:(int)num;

@end

@interface CustomViewController : UIViewController

@property(nonatomic,weak) id<PassValueDelegate>delegate;

@end

在CustomViewController.m中定义一个方法,如果设置了代理,并且代理实现了协议的方法,那么就执行代理方法。

#import "CustomViewController.h"

@interface CustomViewController ()

@end

@implementation CustomViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
} -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.delegate && [self.delegate respondsToSelector:@selector(buyThingNum:)]) {
[self.delegate buyThingNum:];
}
} @end

在ViewController类中,设置代理,实现代理方法。

import "ViewController.h"
#import "CustomViewController.h"
@interface ViewController ()<PassValueDelegate> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
} -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CustomViewController *customVC = [[CustomViewController alloc]init];
customVC.delegate = self;
[self presentViewController:customVC animated:YES completion:nil];
} -(void)buyThingNum:(int)num
{
NSLog(@"%d",num);
} @end

二. 通知

1. 通知的发送: 系统通过一个叫通知中心进行的,通知中心是一个单例。

    NSNotification *notifacation = [NSNotification notificationWithName:@"passValue" object:@"哈哈" userInfo:@{@"num":@""}];
[[NSNotificationCenter defaultCenter]postNotification:notifacation];

 2.接收通知

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buyThingNum:) name:@"passValue" object:nil];
} -(void)buyThingNum:(NSNotification *)noti
{
NSString *wordStr = [noti object]; NSDictionary *dic = [noti userInfo];

  //拿到通知的值
NSLog(@"%@",wordStr);
NSLog(@"%@",dic);

}

3.移除通知

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"passVaule" object:self];

打印结果:

2017-06-24 21:53:01.021 DelegateTest[15607:2194987] 哈哈

2017-06-24 21:53:01.021 DelegateTest[15607:2194987] {

num = 10;

}

 

三、通知与代理的优缺点

(1)通知可以一对多通信,代理只能一对一。

(2)代理的执行效率比较高。

(3)通知的使用比较简单。

(4)通知太多的情况下,代码比较难维护。

iOS---代理、协议、通知 详解的更多相关文章

  1. “iOS 推送通知”详解:从创建到设置到运行

    这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...

  2. ios开发——实用技术OC-Swift篇&本地通知与远程通知详解

    本地通知与远程通知详解 一:本地通知   Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...

  3. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  4. [转帖]技术扫盲:新一代基于UDP的低延时网络传输层协议——QUIC详解

    技术扫盲:新一代基于UDP的低延时网络传输层协议——QUIC详解    http://www.52im.net/thread-1309-1-1.html   本文来自腾讯资深研发工程师罗成的技术分享, ...

  5. 协议分析 - DHCP协议解码详解

    协议分析 - DHCP协议解码详解 [DHCP协议简介]         DHCP,全称是 Dynamic Host Configuration Protocol﹐中文名为动态主机配置协议,它的前身是 ...

  6. Memcache的使用和协议分析详解

    Memcache的使用和协议分析详解 作者:heiyeluren博客:http://blog.csdn.NET/heiyeshuwu时间:2006-11-12关键字:PHP Memcache Linu ...

  7. iOS 单元测试之XCTest详解(一)

    iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...

  8. iOS中—触摸事件详解及使用

    iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...

  9. IOS中UITableViewCell使用详解

    IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...

  10. iOS 后台持续定位详解(支持ISO9.0以上)

    iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...

随机推荐

  1. com/mysql/jdbc/Driver : Unsupported major.minor version 52.0

    解决方案: 1.jdk7+老版5.0驱动com/mysql/jdbc/Driver 2.jdk8+新版6.0驱动com/mysql/cj/jdbc/Driver

  2. tomcat 服务器故障排除

    故障现象 通过浏览器访问tomcat服务器发现服务器没有响应. 问题分析检查 登陆服务器发现,TOMCAT服务器并没有宕机,服务还在. 使用JPS命令查看了一下tomcat的进程ID,获取进程ID后, ...

  3. poj 2506 Tiling(高精度)

    Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tili ...

  4. 20) maven 项目结构:all in one

    这是最常见的项目结构 垂直结构 也是初学者常用的 也是小项目常用的 优点 全部代码在一个项目里,一目了然. 结构简单易于理解 一开始时小巧 缺点 随之而来的缺点也十分明显 前端项目,后端项目,接口项目 ...

  5. CentOS FTP服务器系统套件全面讲解

    对大家推荐很好使用的CentOS FTP系统,像让大家对CentOS FTP系统有所了解,然后对CentOS FTP系统全面讲解介绍,希望对大家有用. 1.vsFTPd,目前常用CentOS FTP服 ...

  6. 周鸿祎与85后的座谈(一):人人需要Mentor,世界没有奇迹

    我们公司现在有将近 4000 人,我觉得里面肯定是卧虎藏龙.百里挑一的话,也有 40 个伟大的人才.但是,即使是天才,如果没有前辈的帮助,最后也会变成庸才,是做不出什么大事的.举例来说,每一个跳水冠军 ...

  7. Java读取修改Properties文件

    properties文件是我们经常需要操作一种文件,它使用一种键值对的形式来保存属性集. 无论在学习上还是工作上经常需要读取,修改,删除properties文件里面的属性. 本文通过操作一个prope ...

  8. AngularJS $observe $watch

    $observe $watch都可以用来监听值的变化,但是他们有显著的区别.$observe是用来监视DOM属性值的变化,而 $watch监视scope属性值的变化.AngularJS中的监听,都知道 ...

  9. UIKit Dynamic主题学习笔记

    1.重力效果:UIGravityBehavior @IBOutlet weak var frogImage: UIImageView! //创建一个关联到view的动画(必须为全局变量) lazy v ...

  10. [Proposal]Nano-Diary(纳日记)

    [Motivation] 很多人都有记日记的习惯,不为别的,就为了那份情怀.但是也有很多人不记日记,原因是嫌写字麻烦.记得很久很久以前,在<读者>上读过一篇文章,大意是一个人用数值记下每天 ...