• NSNotification


    作用:NSNotificationCenter是 专门供程序中不同类间的消息通信而设置的.

    注册通知:即 要在什么地方接受消息

                   [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];

          参数介绍:

              addObserver: 观察者,即在什么地方接收通知;

            selector: 收到通知后调用何种方法;

             name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

    发 送通知:调用观察者处的方法。

               [[NSNotificationCenter defaultCenter] postNotificationName:@"mytest"  object:searchFriendArray];

              参数:

             postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

                     object:传递的参数

    注册方法的写法:

    - (void) mytest:(NSNotification*) notification

    {

       id obj = [notification object];//获 取到传递的对象


    附:注册键盘升启 关闭消息

    1. // 键盘升起
    2. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    3. // 键盘降下
    4. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

    补充:

    NSNotification

      个人觉得用这个东西在不同的viewcontroller间传东西很方便的

      发消息

      [[NSNotificationCenter defaultCenter] postNotificationName:@"popView"/*消息名字,在添加监听时会用到*/       object:@"ShowHomeLineViewController"/*传的参数,多个参数就可以用数组啦*/];

      收消息

      1、添加监听:

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Show:)/*收到消息后的响应函数*/ name:@"popView"/*消息名字,在发消息时  指定的*/ object:nil];

      2、消息处理(实现前面的Show:函数)

      -(void)Show:(NSNotification*)notification

      {

      NSString* str = (NSString*)[notification object];//这里取出刚刚从过来的字符串

      }

      3、不要忘记移除监听

      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"popView" object:nil];

  • 相关阅读:
    《程序员修炼之道》——第二章 注重实效的途径(三)
    《程序员修炼之道》——第二章 注重实效的途径(二)
    《程序员修炼之道》——第二章 注重实效的途径(一)
    win10 磁盘占用高--- 禁用用户改善反馈 CompatTelRunner.exe
    ffmpeg拼接多个音频
    词云-wordcloud
    大数据指数日常应用
    搜索过滤Tip : title,site(搜标题和搜网站)
    eclipse下查看java源码设置
    sqlplus sys as sysdba
  • 原文地址:https://www.cnblogs.com/mobileworld/p/2306505.html
Copyright © 2020-2023  润新知