通知中心 - NSNotificationCenter

本文深入探讨了Objective-C与Swift两种编程语言在iOS开发领域的应用,包括它们各自的优势、特点以及如何在实际项目中进行选择与使用。

---恢复内容开始---

NS_ASSUME_NONNULL_BEGIN

 

/**************** Notifications ****************/

 

// 通知,被发送,被接受。

@interface NSNotification : NSObject <NSCopying, NSCoding>

通知的名字 

@property (readonly, copy) NSString *name;

具体某一个对象。

@property (nullable, readonly, retain) id object;

参数信息

@property (nullable, readonly, copy) NSDictionary *userInfo;

初始化一个 通知 对象

name     将来又谁来接受,(发送给谁)

object    具体的某一个对象,或者为nil,

userInfo  附加的信息,参数

- (instancetype)initWithName:(NSString *)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo NS_AVAILABLE(10_6, 4_0) NS_DESIGNATED_INITIALIZER;

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

 

@end

 

@interface NSNotification (NSNotificationCreation)

初始化一个通知 

+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject;

同上

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

 

- (instancetype)init /*NS_UNAVAILABLE*/; /* do not invoke; not a valid initializer for this class */

 

@end

 

/**************** Notification Center ****************/

 

@interface NSNotificationCenter : NSObject {

    @package

    void * __strong _impl;

    void * __strong _callback;

    void *_pad[11];

}

 单利,获取通知中心,来注册,或者发送通知。一定是 先注册,后发送。不然,无效。

+ (NSNotificationCenter *)defaultCenter;

注册通知,(监听通知) 

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;

 发送通知

- (void)postNotification:(NSNotification *)notification;

- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;

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

 

移除通知 

- (void)removeObserver:(id)observer;

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

 

- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable 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.

 

@end

 

NS_ASSUME_NONNULL_END

---恢复内容结束---

转载于:https://www.cnblogs.com/kinghx/p/5351016.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值