开发中的生活

通知、代理、block的区别和用法

2016-06-02  本文已影响3939人  不是谁的李逍遥

区别

1.NotificationCenter 通知中心:“一对多”,在APP中,很多控制器都需要知道一个事件,应该用通知;

2.delegate 代理委托:

     1,“一对一”,对同一个协议,一个对象只能设置一个代理delegate,所以单例对象就不能用代理;

     2,代理更注重过程信息的传输:比如发起一个网络请求,可能想要知道此时请求是否已经开始、是否收到了数据、数据是否已经接受完成、数据接收失败

3.block(闭包)

     block和delegate一样,一般都是“一对一”之间通信交互,相比代理block有以下特点

    1:写法更简练,不需要写protocol、函数等等

    2,block注重结果的传输:比如对于一个事件,只想知道成功或者失败,并不需要知道进行了多少或者额外的一些信息

    3,block需要注意防止循环引用

用法

1.NotificationCenter 通知中心

消息通知机制顾名思义,在IOS开发中它就是通过消息,来达到通知的目的。我们需要在通知中心注册我们想要监听的消息,当项目中有地方发出这个消息的时候,通知中心会发送给注册这个消息的对象

a. addObserver

```

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getMessageContent:)name:@"Notification_Send_Message"object:nil];

}

```

b.postNotification

```

- (void)sendTheMessage:(NSMutableDictionary*)m_dic{

//    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_Send_Message" object:m_dic];

[[NSNotificationCenterdefaultCenter]postNotificationName:@"Notification_Send_Message"object:m_dicuserInfo:nil];

}

```

c.处理消息

```

- (void)getMessageContent:(NSNotification*)notifi{

NSMutableArray*m_array = (NSMutableArray*)notifi.object;

NSLog(@"++++++%@++++++", m_array);

//........

}

```

d.removeObserver

```

- (void)viewWillDisappear:(BOOL)animated{

[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"Notification_Send_Message"object:nil];

}

```

2.delegate 委托

委托其实是一种设计模式,通俗一点来讲就是当自己有需求要处理但是不方便的时候,就建立一个委托,请别人来帮忙处理。举个例子:“我要给路人甲打电话,但是我不知道李斯的电话号码;我就拜托章三去查询,章三查到后就发短信给了我号码”,章三就是我的委托对象。相信大家在ios开发中经常会看到类似

@protocol(协议)的代码吧!如果我们要实现一个delegate委托,就先要先定义protocol(协议),在指定收到回调的类中(也就是我)去实现协议中的函数(例如收短信),如果没有实现,编译器就会报警告;下面是一个简单的例子,SecondviewController会回调FirstViewController,FirstViewController实现协议中的回调函数:

协议:ViewSelectedDelegate

```

#ifndef ViewSelectedDelegate_h

#define ViewSelectedDelegate_h

@protocolViewSelectedDelegate 

- (void)viewSelectedAtIndex:(int)index;

@end

#endif /* ViewSelectedDelegate_h */

```

FirstViewController:

```

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view from its nib.

self.secondView= [[SecondViewControlleralloc]initWithNibName:@"SecondViewController"bundle:nil];

self.secondView.m_delegate=self;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)viewSelectedAtIndex:(int)index{

NSLog(@"select view at index %d", index);

}

```

SecondViewController:

```

@interfaceSecondViewController : UIViewController

@property(strong,nonatomic)id m_delegate;

- (IBAction)clickTheBtn:(id)sender;

```

```

@implementationSecondViewController

@synthesizem_delegate = _m_delegate;

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view from its nib.

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)clickTheBtn:(id)sender {

[self.m_delegateviewSelectedAtIndex:1];

}

```

3.block(闭包)

Block是一种比较特殊的数据类型,它可以用于两个界面质检传值,也可以对代码封装作为参数传递。block常常结合typedef来使用,用自己定义的类型去创建block显得更加的简单便捷,接下来举例实现一个block回调,将传入的参数加上另一个值后再回调回来:

SecondViewController的实现:

```

#import

//自定义一个block

typedefvoid(^changeValueBlock)(intvalue);

@interfaceSecondViewController : UIViewController

@property(strong,nonatomic) changeValueBlock changeBlock;

@property(assign)intoldValue;

- (void)updateTheValue:(int) oldValueBlock:(changeValueBlock)block;

- (IBAction)clickTheBtn:(id)sender;

@end

```

FirstviewController的实现:

```

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view from its nib.

self.secondView= [[SecondViewControlleralloc]initWithNibName:@"SecondViewController"bundle:nil];

[self.secondViewupdateTheValue:10Block:^(intvalue) {

//回调

NSLog(@"received the new value:%d", value);

}];

}

```

上一篇下一篇

猜你喜欢

热点阅读