码迷,mamicode.com
首页 > 移动开发 > 详细

iOS---代理与协议以及通知的使用

时间:2017-06-24 23:46:33      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:一对一   通用   设置代理   elf   com   mat   object   atomic   多个   

一、代理

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:5];
    }
}

@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:5];
}


-(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:5];
    }
}

@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":@"10"}];
    [[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---代理与协议以及通知的使用

标签:一对一   通用   设置代理   elf   com   mat   object   atomic   多个   

原文地址:http://www.cnblogs.com/huadeng/p/7074656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!