一.系統(tǒng)消息
1.最近項(xiàng)目中發(fā)現(xiàn)通知使用不當(dāng),會(huì)導(dǎo)致通知回調(diào)不是一一對(duì)應(yīng)哲银,有時(shí)候會(huì)回調(diào)兩次,導(dǎo)致綁定監(jiān)聽(tīng)的方法會(huì)多次調(diào)用(我們程序就頁(yè)面回退了多次,因?yàn)槲覀兘壎ǖ氖琼?yè)面回退的方法)
原因:就是通知注冊(cè)和移除不對(duì)應(yīng)導(dǎo)致华匾。
2.如果你的頁(yè)面是 UIView,那很簡(jiǎn)單可以直接有下面一個(gè)函數(shù)就搞定
#pragma mark - keyboard
- (void)willMoveToWindow:(UIWindow *)newWindow //(willMoveToWindow為 UIView接口)注冊(cè)鍵盤相關(guān)通知
{
if (newWindow) {
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object: nil];
}
else
{
[[NSNotificationCenter defaultCenter] removeObserver: self];//移除
}
}
或者(以下是我們自己封裝的接口机隙,就是頁(yè)面顯示完成蜘拉,頁(yè)面消失完成兩個(gè)接口)
- (void)componentDidDisplay
{
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object: nil];
}
- (void)componentDidDisappear
{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
3.如果你的頁(yè)面是 UIViewController
有兩方法 推薦用第一種
//法1(頁(yè)面將要顯示的時(shí)候添加,頁(yè)面將要消失的時(shí)候移出)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object: nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
法2 (頁(yè)面創(chuàng)建的時(shí)候添加 viewDidLoad有鹿, 頁(yè)面銷毀的時(shí)候移出 dealloc)
不推薦的原因是 ARC模式下(MRC一樣)如果block循環(huán)引用或計(jì)時(shí)器之類的使用不當(dāng)旭旭,會(huì)導(dǎo)致dealloc不調(diào)用,也就導(dǎo)致通知不對(duì)應(yīng)
//頁(yè)面創(chuàng)建的時(shí)候添加 viewDidLoad葱跋, 頁(yè)面銷毀的時(shí)候移出 dealloc
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object: nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
2017年1月5日
二.自定義消息
以上頁(yè)面由于發(fā)送的是系統(tǒng)通知UIKeyboardWillShowNotification持寄,不需要調(diào)用postNotificationName
1.自定義消息除了正確參考系統(tǒng)消息寫法外源梭,還要自己發(fā)通知
公共宏頁(yè)面
#define kRrefreshPasswordNotification @"huRefershPassword"
A頁(yè)面//發(fā)通知
[[NSNotificationCenter defaultCenter] postNotificationName:kRrefreshPasswordNotification object:nil];
B頁(yè)面//注冊(cè)消息和方法--初始化時(shí)候?qū)? (參照系統(tǒng)消息寫法)
[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(refreshData:) name: kRrefreshPasswordNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver: self];
-(void)refreshData:(NSNotification*)notification
{
NSDictionary *param = [notification object];//通過(guò)這個(gè)獲取到傳遞的對(duì)象
[self _unlockSubmitButton];
[self reloadBankAndMoney];
}
(void)addNotifictionListener
{
[kNotificationCenter addObserver:self selector:@selector(receiveSocketData:) name:kRefreshPushCourceNotification object:nil];
}(void)removeNotificationListener
{
[kNotificationCenter removeObserver: self];
}
如果您發(fā)現(xiàn)本文對(duì)你有所幫助,如果您認(rèn)為其他人也可能受益际看,請(qǐng)把它分享出去咸产。