zoukankan      html  css  js  c++  java
  • NSNotificationCenter & NSNotification 使用

    NSNotification顾名思义,就是通知用的。

    1. 注册一个监听者;

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        
    [center addObserver: self
               selector: @selector(doGetUserId:)
                   name: @"get_uid"
                 object: nil];

    2. 发送通知;

    [[NSNotificationCenter defaultCenter] postNotificationName: @"get_uid" object: uid];

    3. 接受通知;

    - (void) doGetUserId: (NSNotification *) notification
    {
        if ([[notification name] isEqualToString: @"get_uid"]) {
            NSLog(@"notification is ok.");
            
            NSString *tmp = [NSString stringWithFormat: @"%@", [notification object]];
            
            self.uid = tmp;
            
    //        NSLog(@"%@, %@", userId, self.uid);
            
            userIdLabel.text = self.uid;
        }
    }

     4. 注销通知。

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        
    [center removeObserver: self
                      name: @"get_uid" 
                    object: nil];

    PS:

    1. 刚开始,那个接受命令的函数没有写好,传入的参数必须是NSNotification,换成NSString就会出错,说找不到notification之类的;

    2. 由于获取的uid从一开始就是个NSNumber类型的,就算用NSString的指针来指向,当label.text = Ta时,因为setter中要比较是否相等,NSNumber中没有那个isEqualToString,就会出错。

  • 相关阅读:
    MATLAB 简单多边形的核
    MATLAB Moravec算子
    MATLAB Sepia Tone滤镜
    MATLAB 异或分类
    MATLAB 神经网络分类
    MATLAB 地图上画经纬度
    MATLAB 最小二乘多项式拟合
    MATLAB 对应点集配准的四元数法
    MATLAB 高斯牛顿法最优化
    MATLAB Levenberg-Marquardt法最优化
  • 原文地址:https://www.cnblogs.com/litstrong/p/2566540.html
Copyright © 2011-2022 走看看