各種消息機制使用場景
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也有自己的要求。
- 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
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值