• NSNotificationCenter


    - (void)viewDidLoad {

        [super viewDidLoad];

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(100, 100, 100, 50);

        btn.backgroundColor = [UIColor grayColor];

        [btn setTitle:@"click me" forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];

        

        [self.view addSubview:btn];

        

        // 注册消息

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNoticeMessage:) name:@"test" object:@"notice-recieve"];

    }

     

    -(void)clickAction:(id)sender

    {

        // 能接受消息

        [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:@"notice-recieve"];

        

        // 不能接收到消息,object 对象不一致

        [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:@"notice-recieve111"];

    }

     

    // 处理消息

    -(void)handleNoticeMessage:(id)sender

    {

        NSLog(@"handleNoticeMessage sender = %@", sender);

    }

     

    -(void)viewWillDisappear:(BOOL)animated

    {

        // 移除通知

        [[NSNotificationCenter defaultCenter]removeObserver:self];

    }

  • 相关阅读:
    Pytorch笔记 (2) 初识Pytorch
    Pytorch笔记 (1) 初始神经网络
    c++ 数据抽象 、封装 接口(抽象类)
    c++ 多态
    c++ 重载运算符和重载函数
    c++ 继承
    c++面向对象 —— 类和对象
    c++ 结构
    c++ 基本的输入输出
    c++ 引用 日期&时间
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5190798.html
Copyright © 2020-2023  润新知