可以实现一对多,NSNotification 的实际是一种观察者模式。 NSNotification NSNotification 是 NSNotificationCenter 接收到消息之后根据内部的消息转发表,将消息发送给订阅者封装的对象; @interface NSNotification 属性都是只读的,如果要创建通知则要用下面 NSNotification(NSNotificationCreation) 分类相应的方法进行初始化; NSNotification 不能通过 init 实例化 ,这样会引起下面的异常,比如: NSNotification *notif = [[NSNotification alloc] init]; *** Terminating app due to uncaught block 上面苹果官方给我们提供的方法外,我们还可以利用基于 block 的 NSNotification 去实现,apple 从 ios4 之后提供了带有 block 的 NSNotification
---- NSNotification NSNotification是方便NSNotificationCenter广播到其他对象时的封装对象,简单讲即通知中心对通知调度表中的对象广播时发送NSNotification NSNotification的工作机制 1.应用程序中需要订阅通知的对象,会向通告中心(Notification Center,NSNotificationCenter类的实例)注册,从而成为该事件的监听者 2.监听对象发生变化后,对象给通告中心发一个通告(NSnotification的实例)。该通告对象包括识别通告的标志、发布通告的对象ID和可选的附加信息字典。 @interface NSNotification : NSObject <NSCopying, NSCoding> @property (readonly, copy) NSNotificationName 在通知中心注册观察者,发送者使用通知中心广播时,以NSNotification的name和object来确定需要发送给哪个观察者。
iOS中通知中心NSNotificationCenter应用总结 一、了解几个相关的类 1、NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量。 @property (readonly, copy) NSDictionary *userInfo; NSNotification的初始化方法: - (instancetype)initWithName selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject; 发送通知消息的方法 - (void)postNotification:(NSNotification addObserver:self selector:@selector(notice:) name:@"123" object:nil]; 之后,在我们需要时发送通知消息 //创建一个消息对象 NSNotification * notice = [NSNotification notificationWithName:@"123" object:nil userInfo:@{@"1":@"123"}]; //发送消息
首先看一下比较重要的NSNotification类,这是通知中心的基础,通知中心发送的的通知都会封装成该类的对象进而在不同对象之间传递。 //字典类型的用户信息,用户可将需要传递的数据放入该字典中 @property (nullable, readonly, copy) NSDictionary *userInfo; //下面三个是NSNotification (NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo; NSNotification SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject; /* 发送通知,需要手动构造一个NSNotification 对象 */ - (void)postNotification:(NSNotification *)notification; /* 发送通知 aName为注册的通知名称 anObject为接受通知的对象
@protocol SDKLifeCycleListener <NSObject> @optional - (void)didFinishLaunching:(NSNotification*)notification ; - (void)didBecomeActive:(NSNotification*)notification; - (void)willResignActive:(NSNotification*)notification ; - (void)didEnterBackground:(NSNotification*)notification; - (void)willEnterForeground:(NSNotification *)notification; - (void)willTerminate:(NSNotification*)notification; @end //注册生命周期回调函数 void SDKRegisterLifeCycleListener
@objc func terminateWindow(notification: NSNotification) { ... @objc func terminateWindow(notification: NSNotification) { ... 我帮他家搜寻了一番,终于找到了正解 extension LoginViewController { @objc func onSucceed(notification: NSNotification close() ... } @objc func onFailed(notification: NSNotification) { /// TODO
@objc func terminateWindow(notification: NSNotification) { ... @objc func terminateWindow(notification: NSNotification) { ... 我帮他家搜寻了一番,终于找到了正解 extension LoginViewController { @objc func onSucceed(notification: NSNotification close() ... } @objc func onFailed(notification: NSNotification) { /// TODO
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.sensorStateChange(_:)), name: NSNotification.Name.UIDeviceProximityStateDidChange UIDevice.current.isProximityMonitoringEnabled = false; NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceProximityStateDidChange
NotificationCenter.default.addObserver( self, selector: #selector(reachabilityChanged), name: NSNotification.Name.reachabilityChanged startNotifier() } @objc func reachabilityChanged(notification: NSNotification) { if self.reach!. NotificationCenter.default.addObserver( self, selector: #selector(reachabilityChanged), name: NSNotification.Name.realReachabilityChanged startNotifier(); } @objc func reachabilityChanged(notification: NSNotification) { let status
返回值决定是否允许结束编辑 - (BOOL)textShouldEndEditing:(NSText *)textObject; //已经开始编辑时调用 - (void)textDidBeginEditing:(NSNotification *)notification; //已经结束编辑时调用 - (void)textDidEndEditing:(NSNotification *)notification; //文本改变时调用 - (void )textDidChange:(NSNotification *)notification; //下面这些方法用来快捷创建NSTextField + (instancetype)labelWithString
isKindOfClass:[PayResp class]]){ switch (resp.errCode) { case WXSuccess:{ //支付返回结果,实际支付结果需要去自己的服务器端查询 NSNotification *notification = [NSNotification notificationWithName:@"ORDER_PAY_NOTIFICATION" object:@"success"]; NSNotificationCenter defaultCenter] postNotification:notification]; break; } default:{ NSNotification *notification = [NSNotification notificationWithName:@"ORDER_PAY_NOTIFICATION"object:@"fail"]; ORDER_PAY_NOTIFICATION" object:nil]; 对监听到的消息进行处理: #pragma mark - 收到支付成功的消息后作相应的处理 - (void)getOrderPayResult:(NSNotification
UIKeyboardWillChangeFrameNotification object:nil]; } 3.实现监听 #pragma mark-键盘出现隐藏事件 -(void)keyHiden:(NSNotification CGAffineTransformIdentity; // commentView.hidden = YES; }]; } -(void)keyWillAppear:(NSNotification
, 2) command alias swift_reveal_start expr NotificationCenter.default.post(name: NSNotification.Name( IBARevealRequestStart"), object: nil) command alias swift_reveal_stop expr NotificationCenter.default.post(name: NSNotification.Name
startNotifier]; [self updateInterfaceWithReachability:_reachability]; 实现两个方法: - (void) reachabilityChanged: (NSNotification (Reachability*) curReach;//处理连接改变后的情况 下面是方法的具体实现,可根据情况自行改变: // 连接改变 - (void) reachabilityChanged: (NSNotification
NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame self.view.endEditing(true) } 键盘改变事件,防止键盘遮住输入框 // 键盘改变 func keyboardWillChange(notification: NSNotification
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; return url; } // 播放状态改变 - (void)mediaPlayerPlaybackStateChange:(NSNotification break; default: break; } } // 播放完成 - (void)mediaPlayerPlaybackFinished:(NSNotification stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; return url; } // 播放状态改变 -(void)mediaPlayerPlaybackStateChange:(NSNotification break; default: break; } } // 播放完成 -(void)mediaPlayerPlaybackFinished:(NSNotification
)), name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillShow(notification: NSNotification } ) } } func keyboardWillHide(notification: NSNotification
NSnottificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification (id)object; // 通知发布者(是谁要发布通知) - (NSDictionary *)userInfo; //一些额外的信息(通知发布者传递给通知接收者的信息内容) //初始化一个通知(NSNotification NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo; 发布通知 - (void)postNotification:(NSNotification 监听者 [[NSNotificationCenter defaultCenter] removeObserver:aaa]; iOS监听某些事件的方法 通知(NSNotificationCenter\NSNotification
startNotifier]; [self updateInterfaceWithReachability:_reachability]; 实现两个方法: - (void) reachabilityChanged: (NSNotification (Reachability*) curReach;//处理连接改变后的情况 下面是方法的具体实现,可根据情况自行改变: // 连接改变 - (void) reachabilityChanged: (NSNotification