zoukankan      html  css  js  c++  java
  • 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];

    }

  • 相关阅读:
    浅谈Cauchy不等式
    终于结束的起点——CSP-S 2019 第二轮游记
    LOJ 10172 涂抹果酱
    数字表格
    CSP-S 2019 第一轮 游记
    20191011模拟赛
    Luogu 2327 扫雷
    NOIAC 30 candy
    FormData文件上传
    sde表空间无法导入数据和编辑
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5190798.html
Copyright © 2011-2022 走看看