zoukankan      html  css  js  c++  java
  • iOS NSNotificationCenter 监听

      界面之间的传值的方法有很多,最基本的是可以实例化对象进行传值,也可以使用block块或者是使用通知进行传值。这里记录一下关于通知进行传值:

         首先使用通知你需要在接收方添加一个通知即注册通知监听器,通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)

    - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
        //observer:监听器,即谁要接收这个通知
        //aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入
        //aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知
        //anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
        
        
        - (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
        //name:通知的名称
        //obj:通知发布者
        //block:收到对应的通知时,会回调这个
        //blockqueue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行
        通知
        一个完整的通知一般包含3个属性:
        - (NSString *)name; // 通知的名称
        - (id)object; // 通知发布者(是谁要发布通知)
        - (NSDictionary *)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容)

    初始化一个通知(NSNotification)对象:

    - (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo 
    + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
    + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

    发布通知:发布一个通知可以在notification对象中设置通知的名称、通知的发布者和额外信息等;

    - (void)postNotification:(NSNotification *)notification;
    - (void)postNotificationName:(NSString *)aName object:(id)anObject;
    - (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

    移除通知:

    - (void)removeObserver:(id)observer;
    - (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;

    以上的就是通知所要用到的基本方法,那么下面看一下例子吧!

      *在首页里面需要先添加一个监听器:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title=@"one";
        [self.view setBackgroundColor:[UIColor whiteColor]];
        // Do any additional setup after loading the view.
        
          self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Go CtrlA" style:UIBarButtonItemStylePlain target:self action:@selector(go)];
        //添加一个监听器
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationCenter:) name:@"noti" object:nil];
        
    }

    其次就是实现他所对应的方法:

    -(void)notificationCenter:(NSNotification *)noti{
        NSLog(@"noti---%@",noti.userInfo);
    }

    然后在第二个页面发送通知:

    -(void)btnClickAction:(id)sender{
        NSArray *values=@[@"One",@"Two",@"Three"];
        NSArray *keys=@[@"A",@"B",@"C"];
        NSDictionary *dict =[NSDictionary dictionaryWithObjects:keys forKeys:values];
      //发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:
    @"noti" object:self userInfo:dict]; [self.navigationController popViewControllerAnimated:YES]; }

    另外一个值得注意的就是在使用完通知后,要注意删除通知:

    -(void)viewDidUnload{
        //删除
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeColorKey" object:nil];
    }

    这样一来,通知传值就基本完成了!!!!

            另外还有两个小内容,占块小地方:

     //监听后台
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(viewDidAppear:)
                                                     name:UIApplicationWillResignActiveNotification object:nil];//进入后台
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didNotification) name:UIApplicationDidBecomeActiveNotification object:nil];//退出后台
  • 相关阅读:
    Android获取网络图片应用示例
    VC操作MPP文件
    带"叉叉"的GridView
    持续集成之路 —— Mock对象引起的测试失败
    一次httpserver优化的经验和教训(silverlight游戏
    独立开发人员的自由之路 ——1
    Python标准库:内置函数chr(i)
    Matlab The Bisection Method
    ELF解析(part one)
    文件打开的过程——调用fd=open()时操作系统所做的工作
  • 原文地址:https://www.cnblogs.com/boyuanmeng/p/4730523.html
Copyright © 2011-2022 走看看