iOS中通知中樞NSNotificationCenter應用總結

來源:互聯網
上載者:User

標籤:

通知中樞(NSNotificationCenter)實際是在程式內部提供了一種廣播機制。把接收到的訊息,根據內部的訊息轉寄表,將訊息轉寄給需要的對象。這句話其實已經很明顯的告訴我們要如何使用通知了。第一步:在需要的地方註冊要觀察的通知,第二步:在某地方發送通知。(這裡注意:發送的通知可能是我們自訂的,也可能是系統的)。

一,使用通知

第1中建立通知方法

//注意,通知的使用是有先後順序的

//一定要先監聽通知,然後在發送通知

 //第一種方法    // 添加一個通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil];        //發送一個通知    [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
  //發送通知也可以傳遞一些參數    [NSNotificationCenter defaultCenter] postNotificationName:<#(nonnull NSNotificationName)#> object:<#(nullable id)#> userInfo:<#(nullable NSDictionary *)#>

 

可以在監聽的通知的方法擷取通知的資訊

- (void)addNotification {    NSLog(@"接受通知");}

最後要移除通知

- (void)dealloc {    //移除觀察者 self    [[NSNotificationCenter defaultCenter] removeObserver:self];}

 

第2種通知的使用使用 block 比較方便簡單

這個方法需要一個id類型的值接受

@property (nonatomic, weak) id observe;

 

    //第二種方法    //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];    

同樣,這裡也需要移除通知,但是這裡的觀察者不是 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    });}

控制台輸出 : 非同步

2016-10-07 15:17:05.537 通知多線程使用[5798:299202] 接受通知2016-10-07 15:17:05.538 通知多線程使用[5798:299202] <NSThread: 0x60800026d8c0>{number = 4, name = (null)}

主線程中發送通知,非同步線程中監聽通知

- (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]);        //非同步:監聽通知,主線程發送通知    //總結: 接受通知的代碼由發出通知的線程}

控制台輸出

2016-10-07 15:28:40.160 通知多線程使用[5879:321079] 接受通知2016-10-07 15:28:40.160 通知多線程使用[5879:321079] <NSThread: 0x600000072ac0>{number = 1, name = main}

使用 block 建立通知的方法,

queue :[NSOperationQueue mainQueue] 這樣都會在主線程中執行block 中代碼
    _observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {        NSLog(@"收到了通知");    }];

控制台輸出 : 主線程中執行

2016-10-07 15:36:39.277 通知多線程使用[5979:332663] 收到了通知2016-10-07 15:36:39.277 通知多線程使用[5979:332663] <NSThread: 0x60000006ea40>{number = 1, name = main}

 

iOS中通知中樞NSNotificationCenter應用總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.