消息机制一点点思考

转载自:http://www.jianshu.com/p/6da32fa8841c

自我总结 (方法步骤):

一、协议和代理

 我:    1.声明协议方法

      2.声明代理属性

      3.调用协议方法

 

办事员 :    1.遵守协议

        2.想法设置自己为代理

        3.实现协议方法

 

二、block 作为对象属性 Replace Delegate代理方法

 我:       1.设置Block 对象属性

         2.在我的方法中调用block

 

办事员:  1.实现block

 

三:block 作为参数 Replace Delegate 方法

我:        1.设置Block实例变量

          2.带block参数方法中 赋值block实现地址给block实例变量

      3.调用block

 

办事员       1.实现带block参数方法

 

 

 

这里有一篇介绍详细的文章 objc io 中文版

各种消息机制使用场景
各种消息机制使用场景

Target-Action

这是最典型的一种消息机制。最常见的情况就是,点击 view 中 button时,会触发 controller 中函数。

  • Target-Action 的一个限制是,Target-Action 的参数不能自定义,一般情况下参数为action的发送者。
  • Target-Action间为松耦合关系。recipient 在接受到消息前,并不知道sender是谁(多个控件可以与同一个函数关联);sender 在发送前也不知 recipient 是谁(若在父类中定义action,action将从子类传到父类中响应( responder chain ))。

delegate

delegate 在 iOS开发中经常被用到,功能与使用类似于C中的回调函数。发送者需要知道接受者,接受者可以不知道发送者。最常见的情况就是 tableview 的使用:使用tableview的时候,设置 datasource 与 delegate,就相当于tableview这个发送者知道接受者信息;此 tableview 的 controller 中也必须实现相关 protocol 。

  • 使用delegate最大的好处,就是可以定义任何方法,可以根据需求来传递消息;发送者也可以通过接受者的返回值来作出响应

Block

Block 是 iOS 中,实现函数回调的第二种方法,第一种就是上面说的 delegate 。Block可以代替delegate使用,即将一个 Block 作为一个 property 。并且还可以用Block作为参数来替代delegate (设置实例变量来接收block实现地址---- 如果你知道我在说什么的话)

Block也有自己的要求。

  • Block中要避免产生 retain cycle 。
  • Block可以增强代码大可读性。多用在动作完成的回调、错误的回调等类似的事情。

Notification

这是经典的生产者-发送者模型之一。 Notification 的一个优点是,消息发送者与接受者不需要知道对方,可以在两个互不相同的模块中使用。

  • Notification 可以用来发送任何消息,可以将要发送的消息内容放在 userinfo字典中。
  • Notification 的传递是单向的,不能回复通知。
    这里实现一个小 demo。主要功能是,在输入框中输入数字,当输入为5~10时,在label中打印出输入值。

//消息名称
static NSString *inputNotifciation = @"linw.test.notification";
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_inputField becomeFirstResponder];
//注册消息接受者及调用函数
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotif:) name:inputNotifciation object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidAppear:animated];
//移除消息观察者
[[NSNotificationCenter defaultCenter] removeObserver:self name:inputNotifciation object:nil];
}

//按钮动作
- (IBAction)sendButton:(UIButton *)sender
{
NSInteger inputNum = _inputField.text.integerValue;
if (inputNum >= 5 && inputNum < 10) {
NSLog(@"post notification");
[[NSNotificationCenter defaultCenter] postNotificationName:inputNotifciation object:self userInfo:@{@"Num" : [NSNumber numberWithInteger:inputNum]}];
}
else
{
_targetLabel.text = @"input is invaild";
}
_inputField.text = @"";
}

//收到消息后调用函数
- (void)receiveNotif:(NSNotification *)notif
{
NSLog(@"receive notification");
_targetLabel.text = [[NSString alloc] initWithFormat:@"Receive notification: %@", notif.userInfo[@"Num"]];
[_inputField resignFirstResponder];
}
@end

初始状态
初始状态
输入非法
输入非法
输入5~10
输入5~10

KVO

KVO是另外一种生产者-消费者模式,当一个对象值被改变时,另一个对此对象感兴趣的对象,将得到通知。实现原理,当使用KVO时,编译器会自动生成一个子类,子类中重写方法,实现消息通知。

  • 消息接收者需要知道被观察者。
  • 消息接收者也需要知道被观察者生命周期。需要在被观察者被 dealloc 前,注销观察者身份,不然会出现未预料的错误。

以下是一个小demo。功能如下:当按下 add 5按钮时,改变被观察者值(price,每次加5),在消息通知函数中,改变 textfield 内容显示当前 price。

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField priceText;
@property (nonatomic) float price;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_priceText.text = @"15.00";
//设置被观察的属性
[self setValue:@"15.0" forKey:@"price"];
//添加观察者
[self addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
//消息通知函数
- (void)observeValueForKeyPath:(NSString
)keyPath ofObject:(id)object change:(NSDictionary )change context:(void )context
{
if ([keyPath isEqualToString:@"price"])
{
NSLog(@"receive KVO notification");
//_priceText.text = [[NSString alloc] initWithFormat:@"%f", ((NSNumber )[self valueForKey:@"price"]).doubleValue];
_priceText.text = [[NSString alloc] initWithFormat:@"%.2f", ((NSNumber
)change[NSKeyValueChangeNewKey]).floatValue];
}
}
//add 5 button action
- (IBAction)addFiveStockPrice:(id)sender
{
//[self setValue:@"20.0" forKey:@"price"];
_price = _price + 5.00;
[self setValue:[[NSString alloc] initWithFormat:@"%f", _price] forKey:@"price"];
}
- (void)dealloc
{
[self removeObserver:self forKeyPath:@"price"];
}
@end

效果如下:

初始状态
初始状态
按下 add 5后,修改price值
按下 add 5后,修改price值

以上是对iOS消息机制的一些理解。

posted on 2016-01-18 14:19  Jenaral  阅读(232)  评论(0)    收藏  举报

导航