你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS告訴中心(NSNotificationCenter)

IOS告訴中心(NSNotificationCenter)

編輯:IOS開發綜合

告訴中心(NSNotificationCenter)實踐是在順序外部提供了一種播送機制。把接納到的音訊,依據外部的音訊轉發表,將音訊轉發給需求的對象。這句話其實曾經很分明的通知我們要如何運用告訴了。第一步:在需求的中央注冊要察看的告訴,第二步:在某中央發送告訴。(這裡留意:發送的告訴能夠是我們自定義的,也能夠是零碎的)。d

//留意,告訴的運用是有先後順序的

//一定要先監聽告訴,然後在發送告訴

1、NSNotification 這個類可以了解為一個音訊對象,其中有三個成員變量。這個成員變量是這個音訊對象的獨一標識,用於區分音訊對象。

@property(readonly,copy)NSString*name;

這個成員變量定義一個對象,可以了解為針對某一個對象的音訊。

@property(readonly,retain)idobject;

這個成員變量是一個字典,可以用其來停止傳值。

@property(readonly,copy)NSDictionary*userInfo;

readonly 都是只讀的,不能初始化

NSNotification的初始化辦法:

- (instancetype)initWithName:(NSString*)name object:(id)object userInfo:(NSDictionary*)userInfo;

+ (instancetype)notificationWithName:(NSString*)aName object:(id)anObject;

+ (instancetype)notificationWithName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;

留意:官方文檔有明白的闡明,不可以運用init停止初始化

2、NSNotificationCenter

這個類是一個告訴中心,運用單例設計,每個使用順序都會有一個默許的告訴中心。就是用來接納音訊的

添加一個察看者,可以為它指定一個辦法,名字和對象。承受到告訴時,執行辦法。

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString*)aName object:(id)anObject;

發送告訴音訊的辦法

- (void)postNotification:(NSNotification*)notification;

- (void)postNotificationName:(NSString*)aName object:(id)anObject;

- (void)postNotificationName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;

移除察看者的辦法

- (void)removeObserver:(id)observer;

- (void)removeObserver:(id)observer name:(NSString*)aName object:(id)anObject;

運用====================

//1、獲取告訴中心,注冊一個察看者和事情

NSNotificationCenter*center = [NSNotificationCenterdefaultCenter];

//在告訴中心添加一個察看者和一個察看的事情

//參數1.擔任呼應事情的察看對象

//參數2.接納到音訊,察看者執行的辦法

//參數3.察看者要監聽的事情

//參數4.可以限定音訊的發送者,假如為空就表名接納任何音訊

[centeraddObserver:selfselector:@selector(receiveNotification:)name:@"ChangeColor"object:nil];

移除察看者

//只需注冊一個察看者,一定要在dealloc辦法中移除掉自己察看者身份,ARC中也要這麼做,切記!!!

[[NSNotificationCenterdefaultCenter]removeObserver:self];



//收到告訴中心的音訊時,察看者(self)要調用的辦法

- (void)receiveNotification:(NSNotification*)message{

NSLog(@"zzz®%@", message.name);

NSLog(@"rrr%@", message.object);

}


//向告訴中心發送一個音訊

NSNotificationCenter*center = [NSNotificationCenterdefaultCenter];

NSNotification*notice = [NSNotificationnotificationWithName:@"ChangeColor"object:@"good"userInfo:nil];

//參數1.發送音訊的事情

//參數2.可以運用這個參數傳遞一個對象給察看者

//參數3.一些音訊的參數信息(零碎用的比擬多)

[centerpostNotification:notice];



=======================================

第2種告訴的運用運用 block 比擬方便復雜

這個辦法需求一個id類型的值承受

@property (nonatomic, weak) id observe;

IOS通知中心(NSNotificationCenter)

    //第二種辦法
    //Name: 告訴的稱號
    //object:誰收回的告訴
    //queue: 隊列,決議 block 在哪個線程中執行, nil  在發布告訴的線程中執行
    //usingBlock: 只需監聽到告訴,就會執行這個 block
    //這個告訴前往一個 id 這個告訴異樣需求移除
   _observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        NSLog(@"收到了告訴");
    }];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
    
IOS通知中心(NSNotificationCenter)

異樣,這裡也需求移除告訴,但是這裡的察看者不是 self 而是 _observe

- (void)dealloc {

    //移除察看者 _observe
    [[NSNotificationCenter defaultCenter] removeObserver:_observe];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {


//發送告訴
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
});
}


- (void)viewDidLoad {
[super viewDidLoad];

//多線程中運用告訴

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil];

}


- (void)addNotification {
NSLog(@"承受告訴");
NSLog(@"%@",[NSThread currentThread]);

//主線程:監聽告訴,異步發送告訴
//總結: 承受告訴的代碼由收回告訴的線程
//更新 UI
dispatch_sync(dispatch_get_main_queue(), ^{
//更新 UI
});
}



====================

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

//主線程中發送告訴
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
}


- (void)viewDidLoad {
[super viewDidLoad];

//多線程中運用告訴
//異步監聽告訴

dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil];
});
}


- (void)addNotification {
NSLog(@"承受告訴");
NSLog(@"%@",[NSThread currentThread]);

//異步:監聽告訴,主線程發送告訴
//總結: 承受告訴的代碼由收回告訴的線程


}




【IOS告訴中心(NSNotificationCenter)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved