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];

    }

  • 相关阅读:
    HDU 4763 Theme Section(KMP+枚举公共前后缀)
    HDU 3613 Best Reward(扩展KMP求前后缀回文串)
    HDU 3613 Best Reward(manacher求前、后缀回文串)
    扩展KMP模板
    Vue
    Vue
    css 文本换行 文本溢出隐藏用省略号表示剩下内容
    sublime 神一样的插件
    markdown
    css 居中布局
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5190798.html
Copyright © 2011-2022 走看看