• 通知PostNotification


    //
    //  ViewController.m
    //  FKLTest
    //
    //  Created by kun on 16/8/10.
    //  Copyright © 2016年 kun. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "FKLTool.h"
    #import "XMGDownloadTool.h"
    #import "PostNotificationViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSLog(@"******#receiveNotification--Begin#******");
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification) name:@"postNotification" object:nil];
        NSLog(@"******#receiveNotification--End#******");
    
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        PostNotificationViewController *postVc = [[PostNotificationViewController alloc] init];
        
        [self presentViewController:postVc animated:YES completion:nil];
    }
    
    - (void)receiveNotification
    {
        NSLog(@"---begin---%s", __func__);
        sleep(5.0);
        NSLog(@"---after---%s", __func__);
    }
    
    @end
    //
    //  PostNotificationViewController.m
    //  FKLTest
    //
    //  Created by kun on 16/8/24.
    //  Copyright © 2016年 kun. All rights reserved.
    //
    
    #import "PostNotificationViewController.h"
    
    @interface PostNotificationViewController ()
    
    @end
    
    @implementation PostNotificationViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"******#postNotification--Begin#******");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"postNotification" object:nil];
        NSLog(@"******#postNotification--End#******");
    }
    
    @end
    2016-08-24 18:54:07.713 FKLTest[1186:24510] ******#receiveNotification--Begin#******
    2016-08-24 18:54:07.714 FKLTest[1186:24510] ******#receiveNotification--End#******
    2016-08-24 18:56:15.805 FKLTest[1186:24510] ******#postNotification--Begin#******
    2016-08-24 18:56:15.806 FKLTest[1186:24510] ---begin----[ViewController receiveNotification]
    2016-08-24 18:56:20.807 FKLTest[1186:24510] ---after----[ViewController receiveNotification]
    2016-08-24 18:56:20.808 FKLTest[1186:24510] ******#postNotification--End#******

    分析:监听通知必须要在发送通知之前设置好。发送通知,如果有监听此通知的观察者,会等待观察者的监听方法处理完后才会返回;如果没有则立马返回。

  • 相关阅读:
    第一次作业 —— 【作业7】问卷调查
    讲座观后感
    学习进度表(随缘更新)
    数据结构与算法思维导图
    作业七问卷调查
    《创新者的逆袭,用第一性原理做颠覆式创新》读后感
    结对项目--四则运算生成器(Java) 刘彦享+龙俊健
    个人项目---WordCount实现(Java)
    自我介绍+软工五问
    简洁又快速地处理集合——Java8 Stream(下)
  • 原文地址:https://www.cnblogs.com/fkunlam/p/5804277.html
Copyright © 2020-2023  润新知