iOS 通知 3種使用方式,

NSNotification 的3種使用方式

  • 1、不傳遞參數(shù), 最常用的一種
// 發(fā)送通知
-(void)btn1Click{
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
}

//注冊(cè)通知(接收,監(jiān)聽(tīng),一個(gè)通知)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification1) name:@"notifyName1" object:nil];

//實(shí)現(xiàn)方法
-(void)notification1{
NSLog(@"接收 不帶參數(shù)的消息");
}
  • 2、使用object 傳遞消息
//發(fā)通知
-(void)btn2Click:(UIButton *)btn{
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName2" object:[NSString stringWithFormat:@"%@",btn.titleLabel.text]];
}

//注冊(cè)通知(接收,監(jiān)聽(tīng),一個(gè)通知)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification2:) name:@"notifyName2" object:nil];

//實(shí)現(xiàn)方法
-(void)notification2:(NSNotification *)noti{

//使用object處理消息
NSString *info = [noti object];
NSLog(@"接收 object傳遞的消息:%@",info);

}

  • 3、使用userInfo 傳遞消息
//發(fā)通知

-(void)btn3Click{

NSDictionary *dic = [NSDictionary dictionaryWithObject:@"userInfo消息" forKey:@"param"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"nitifyName3" object:nil userInfo:dic];

}

//注冊(cè)通知(接收,監(jiān)聽(tīng),一個(gè)通知)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification3:) name:@"nitifyName3" object:nil];

//實(shí)現(xiàn)方法
-(void)notification3:(NSNotification *)noti{

//使用userInfo處理消息
NSDictionary  *dic = [noti userInfo];
NSString *info = [dic objectForKey:@"param"];
NSLog(@"接收 userInfo傳遞的消息:%@",info);

}

最后,在[接收]消息的頁(yè)面,在dealloc方法里面移除觀察者。

-(void)dealloc{

//移除觀察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
  • [拓展] 在一個(gè)大的項(xiàng)目中, 由于通知較多, 一般建立一個(gè)文件統(tǒng)一管理通知名, 通知名定義為常量

  • NotifyConst.h
#import <Foundation/Foundation.h>

// extern : 引用本類或者其他類中使用
// xxA類通知
extern NSString *const NotifyName1;
extern NSString *const NotifyName2;

// xxB類通知
extern NSString *const xxx
extern NSString *const xxx

//xxC類通知
extern NSString *const xxx;

  • NotifyConst.m
/**
 常量值
 .m 文件中進(jìn)行賦值  const:標(biāo)識(shí)后面的東西是不可修改的
 */

// xxA類通知
NSString *const NotifyName1 = @"NotifyName1";
NSString *const NotifyName2 = @"NotifyName2";

// xxB類通知
NSString *const xxx = ...'
NSString *const xxx = ....

最后, 在pch文件中加入NotifyConst.h就行了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 在多數(shù)移動(dòng)應(yīng)用中任何時(shí)候都只能有一個(gè)應(yīng)用程序處于活躍狀態(tài),如果其他應(yīng)用此刻發(fā)生了一些用戶感興趣的那么通過(guò)通知...
    莫離_焱閱讀 6,708評(píng)論 1 8
  • 原文鏈接http://www.cnblogs.com/kenshincui/p/4186022.html 音頻在i...
    Hyman0819閱讀 22,137評(píng)論 4 74
  • 1.自定義控件 a.繼承某個(gè)控件 b.重寫(xiě)initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,694評(píng)論 2 4
  • 1、 我今天很不高興,趕電梯竟然擠了兩次沒(méi)上去,眼巴巴瞅著心里上火啊,想著一不做二不休爬樓梯算了,腳下五厘米高跟鞋...
    凌格子閱讀 488評(píng)論 0 0
  • 把生活分成很多部分來(lái)做,每一個(gè)部分都不要后悔,每天反思自己是否盡力,是否追求寧?kù)o
    瑪索閱讀 237評(píng)論 0 0

友情鏈接更多精彩內(nèi)容