zoukankan      html  css  js  c++  java
  • Notifications

    Cocoa中,通知中心实现的观察者设计模式。通过下面两个类实现通知的发送和接收。

    /****************    Notifications    ****************/
    
    @interface NSNotification : NSObject <NSCopying, NSCoding>
    
    - (NSString *)name;
    - (id)object; // 发送该通知的对象
    - (NSDictionary *)userInfo; // 额外的可选信息
    
    @end
    
    @interface NSNotification (NSNotificationCreation)
    
    + (id)notificationWithName:(NSString *)aName object:(id)anObject;
    + (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
    
    @end
    /****************    Notification Center    ****************/
    
    @interface NSNotificationCenter : NSObject {
        @package
        void * __strong _impl;
        void * __strong _callback;
        void *_pad[11];
    }
    
    + (id)defaultCenter;
        
    - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
    
    // 发送通知,anObject:发送通知的对象,Observer通过- object方法可以访问该对象
    - (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; #if NS_BLOCKS_AVAILABLE - (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0); // The return value is retained by the system, and should be held onto by the caller in // order to remove the observer with removeObserver: later, to stop observation. #endif @end 
  • 相关阅读:
    菜鸟学IT之豆瓣爬取初体验
    菜鸟学IT之python网页爬取多页爬取
    菜鸟学IT之python网页爬取初体验
    菜鸟学IT之python词云初体验
    菜鸟学IT之python3关于列表,元组,字典,集合浅认识!
    整合ssm框架
    MyBatis
    服务出现服务名无效的原因及解决方法
    数据库(三)
    数据库(二)
  • 原文地址:https://www.cnblogs.com/iprogrammer/p/3248935.html
Copyright © 2011-2022 走看看