
Target-Action
這是最典型的一種消息機(jī)制。最常見的情況就是,點(diǎn)擊 view 中 button時(shí),會(huì)觸發(fā) controller 中函數(shù)。
- Target-Action 的一個(gè)限制是,Target-Action 的參數(shù)不能自定義,一般情況下參數(shù)為action的發(fā)送者。
- Target-Action間為松耦合關(guān)系。recipient 在接受到消息前,并不知道sender是誰(shuí)(多個(gè)控件可以與同一個(gè)函數(shù)關(guān)聯(lián));sender 在發(fā)送前也不知 recipient 是誰(shuí)(若在父類中定義action,action將從子類傳到父類中響應(yīng)( responder chain ))。
delegate
delegate 在 iOS開發(fā)中經(jīng)常被用到,功能與使用類似于C中的回調(diào)函數(shù)。發(fā)送者需要知道接受者,接受者可以不知道發(fā)送者。最常見的情況就是 tableview 的使用:使用tableview的時(shí)候,設(shè)置 datasource 與 delegate,就相當(dāng)于tableview這個(gè)發(fā)送者知道接受者信息;此 tableview 的 controller 中也必須實(shí)現(xiàn)相關(guān) protocol 。
- 使用delegate最大的好處,就是可以定義任何方法,可以根據(jù)需求來(lái)傳遞消息;發(fā)送者也可以通過(guò)接受者的返回值來(lái)作出響應(yīng)。
Block
Block 是 iOS 中,實(shí)現(xiàn)函數(shù)回調(diào)的第二種方法,第一種就是上面說(shuō)的 delegate 。Block可以代替delegate使用,即將一個(gè) Block 作為一個(gè) property 。
Block也有自己的要求。
- Block中要避免產(chǎn)生 retain cycle 。
- Block可以增強(qiáng)代碼大可讀性。多用在動(dòng)作完成的回調(diào)、錯(cuò)誤的回調(diào)等類似的事情。
Notification
這是經(jīng)典的生產(chǎn)者-發(fā)送者模型之一。 Notification 的一個(gè)優(yōu)點(diǎn)是,消息發(fā)送者與接受者不需要知道對(duì)方,可以在兩個(gè)互不相同的模塊中使用。
- Notification 可以用來(lái)發(fā)送任何消息,可以將要發(fā)送的消息內(nèi)容放在 userinfo字典中。
- Notification 的傳遞是單向的,不能回復(fù)通知。
這里實(shí)現(xiàn)一個(gè)小 demo。主要功能是,在輸入框中輸入數(shù)字,當(dāng)輸入為5~10時(shí),在label中打印出輸入值。
//消息名稱
static NSString *inputNotifciation = @"linw.test.notification";
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_inputField becomeFirstResponder];
//注冊(cè)消息接受者及調(diào)用函數(shù)
[[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];
}
//按鈕動(dòng)作
- (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 = @"";
}
//收到消息后調(diào)用函數(shù)
- (void)receiveNotif:(NSNotification *)notif
{
NSLog(@"receive notification");
_targetLabel.text = [[NSString alloc] initWithFormat:@"Receive notification: %@", notif.userInfo[@"Num"]];
[_inputField resignFirstResponder];
}
@end



KVO
KVO是另外一種生產(chǎn)者-消費(fèi)者模式,當(dāng)一個(gè)對(duì)象值被改變時(shí),另一個(gè)對(duì)此對(duì)象感興趣的對(duì)象,將得到通知。實(shí)現(xiàn)原理,當(dāng)使用KVO時(shí),編譯器會(huì)自動(dòng)生成一個(gè)子類,子類中重寫方法,實(shí)現(xiàn)消息通知。
- 消息接收者需要知道被觀察者。
- 消息接收者也需要知道被觀察者生命周期。需要在被觀察者被 dealloc 前,注銷觀察者身份,不然會(huì)出現(xiàn)未預(yù)料的錯(cuò)誤。
以下是一個(gè)小demo。功能如下:當(dāng)按下 add 5按鈕時(shí),改變被觀察者值(price,每次加5),在消息通知函數(shù)中,改變 textfield 內(nèi)容顯示當(dāng)前 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";
//設(shè)置被觀察的屬性
[self setValue:@"15.0" forKey:@"price"];
//添加觀察者
[self addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
//消息通知函數(shù)
- (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
效果如下:

