1、每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信; 2、任何一个对象都可以向通知中心发布通知(NSNotification), 描述自己在做什么 personWithName:@"李四"]; Person *per5 = [Person personWithName:@"王五"]; //取得通知中心对象 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; //设置两个通知的名称 NSString *notice1 additional setup after loading the view, typically from a nib. 4 5 //监听开始编辑文本框 6 [[NSNotificationCenter tanChangeText) name:UITextFieldTextDidChangeNotification object:nil]; 10 11 //监听结束文本框编辑 12 [[NSNotificationCenter
---- NSNotification NSNotification是方便NSNotificationCenter广播到其他对象时的封装对象,简单讲即通知中心对通知调度表中的对象广播时发送NSNotification NSNotification的工作机制 1.应用程序中需要订阅通知的对象,会向通告中心(Notification Center,NSNotificationCenter类的实例)注册,从而成为该事件的监听者 # NSNotificationCenter NSNotificationCenter是类似一个广播中心站,使用defaultCenter来获取应用中的通知中心,它可以向应用任何地方发送和接收通知。 如果使用基于-[NSNotificationCenter addObserverForName:object:queue:usingBlock:]方法在获取方法返回的观察者进行释放。 基于这个方法我们还可以让观察者接到通知后只执行一次: __block __weak id<NSObject> observer = [[NSNotificationCenter defaultCenter
override func viewDidLoad() { super.viewDidLoad() nameObserver = NSNotificationCenter.defaultCenter 析构方法永远不会被执行 } override func viewDidLoad() { super.viewDidLoad() nameObserver = NSNotificationCenter.defaultCenter func viewDidLoad() { super.viewDidLoad() weak var welf = self nameObserver = NSNotificationCenter.defaultCenter 要记得,这个 Observer 其实还被 NSNotificationCenter 引用着,他也是不会释放的,最明显的体现就是,就算你的ViewController被释放了,block中的代码块仍然会被执行 deinit { if let nameObserver = nameObserver { NSNotificationCenter.defaultCenter(
iOS中通知中心NSNotificationCenter应用总结 一、了解几个相关的类 1、NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量。 NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo; 注意:官方文档有明确的说明,不可以使用init进行初始化 2、NSNotificationCenter 二、通知的使用流程 首先,我们在需要接收通知的地方注册观察者,比如: //获取通知中心单例对象 NSNotificationCenter * center = [NSNotificationCenter NSNotification notificationWithName:@"123" object:nil userInfo:@{@"1":@"123"}]; //发送消息 [[NSNotificationCenter
KVC 使用方法详解及底层实现 KVO 正确使用姿势进阶及底层实现 Protocol与Delegate 使用方法详解 NSNotificationCenter 通知使用方法详解 KVO、Delegate 、Notification 区别及相关使用场景 NSNotificationCenter 通知的使用方法详解 NSNotificationCenter通知中心是iOS程序内部的一种消息广播的实现机制,可以在不同对象之间发送通知进而实现通信 通知中心,通知中心采用单例的模式,整个系统只有一个通知中心,通过如下代码获取: [NSNotificationCenter defaultCenter] 再看一下通知中心的几个核心方法: /* 注册通知监听器 的使用步骤如下: 1、在需要监听某通知的地方注册通知监听器 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector 发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"inputTextValueChangedNotification"
1 NSNotificationCenter机制 1.1 原理 1.1.1 不移除通知挂机原因分析 @implementation MRCObject - (id)init { if (self = [super init]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( 我们可以找找看,你代码里面没有remove操作,但是NSNotificationCenter那边已经移除了,不然肯定会出现上面野指针的问题。 意思很简单,NSNotificationCenter消息的接受线程是基于发送消息的线程的。 3 参考链接 iOS NSNotificationCenter使用姿势详解 http://www.jianshu.com/p/a4d519e4e0d5
当我调用sdk-确认授权-触发回调的时候 KVO这两个登录和绑定监听同时触发 [[NSNotificationCenter defaultCenter] postNotificationName :@"weChatLoing" object:dict]; [[NSNotificationCenter defaultCenter] postNotificationName:@"weChatBinding " object:dict]; 问题分析: 基本上确定上内存泄漏,问题是泄漏点在哪,自此开始了漫长的排查 尝试解决: 从dealloc开始入手 - (void)dealloc { [[NSNotificationCenter { [super viewDidLoad]; [self 网络请求]; [self initUI]; [self 登录:@"1" 密码:@"1"]; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(登录监听) name:@"微信监听" object:nil]; // [[NSNotificationCenter
NSNotificationCenter NSNotificationCenter 就相当于一个广播站,使用 [NSNotificationCenter defaultCenter] 来获取,NSNotificationCenter NSNotificationCenter 是整个通知机制的关键所在,它管理着监听者的注册和注销,通知的发送和接收。 NSNotificationCenter 维护着一个通知的分发表,把所有通知发送者发送的通知,转发给对应的监听者们。 NSNotificationCenter 是基于观察者模式设计的,不能跨应用程序进程通信,当 NSNotificationCenter 接收到消息之后会根据内部的消息转发表,将消息发送给订阅者;它可以向应用任何地方发送和接收通知 NSNotificationQueue 就像一个缓冲池把一个个通知放进池子中,使用特定方式通过 NSNotificationCenter 发送到相应的观察者。
AutoPurgeCache - (nonnull instancetype)init { self = [super init]; if (self) { #if SD_UIKIT [[NSNotificationCenter UIApplicationDidReceiveMemoryWarningNotification object:nil]; #endif } return self; } - (void)dealloc { #if SD_UIKIT [[NSNotificationCenter @end 可以看到,内存缓存类AutoPurgeCache里有一个接收系统通知,如有内存报警,会移除全部 3、清理Disk缓存 当 App 进入关闭或进入后台时,清理磁盘缓存 [[NSNotificationCenter UIApplicationDidReceiveMemoryWarningNotification object:nil]; [[NSNotificationCenter UIApplicationWillTerminateNotification object:nil]; [[NSNotificationCenter
#import <React/RCTEventEmitter.h> #define RNIOSExportJsToReact(noti) [[NSNotificationCenter defaultCenter } - (void)startObserving { [[NSNotificationCenter defaultCenter] addObserver:self event-emitted" object:nil]; } - (void)stopObserving { [[NSNotificationCenter notification.object]; } + (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload { [[NSNotificationCenter
mediaPlayerPlaybackFinished:(NSNotification *)noti{ NSLog(@"播放结束"); } // 添加观察者监听视频状态 - (void)addNotification{ NSNotificationCenter *noti = [NSNotificationCenter defaultCenter]; [noti addObserver:self selector:@selector(mediaPlayerPlaybackStateChange viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // 每次退出播放界面都清空,防止再次点击不播放 [[NSNotificationCenter *noti = [NSNotificationCenter defaultCenter]; [noti addObserver:self selector:@selector(mediaPlayerPlaybackStateChange MPMoviePlayerPlaybackDidFinishNotification object:self.playerViewController.moviePlayer]; } -(void)dealloc{ [[NSNotificationCenter
anObject; //一般在监听器销毁之前取消注册(如在监听器中加入下列代码): - (void)dealloc { //[super dealloc]; 非ARC中需要调用此句 [[NSNotificationCenter defaultCenter] removeObserver:self]; } 通知的举例(代码) AAA是通知发布者 aaa是监听者 //aaa监听AAA的<军事新闻>通知 [[NSNotificationCenter defaultCenter] addObserver:aaa selector:@selector(gotNews:) name:@"军事新闻" object:AAA]; //AAA发布<军事新闻通知> [[NSNotificationCenter defaultCenter] postNotificationName:@"军事新闻" object:AAA userInfo:@{@"title" : @"453543"}]; //销毁aaa监听者 [[NSNotificationCenter defaultCenter] removeObserver:aaa]; iOS监听某些事件的方法 通知(NSNotificationCenter\NSNotification) 任何对象之间都可以传递消息
之前我采用把View放到scrollView上来处理,但是后来发现,让View跟着键盘动起来效果更好 // 首先注册通知,弹出键盘和键盘回收两个 // 弹出键盘时view向上偏移,避免遮盖输入框 [[NSNotificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter animations:^{ self.frame = toRect; }]; } } // 最后注销通知 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter
*notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter removeObserver: [self.activityIndicatorView stopAnimating]; }); } #pragma mark - - (void)dealloc { NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter removeObserver - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task { NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; // 先移除对上个任务的通知监听 [notificationCenter removeObserver
UIColor redColor]; [self.view addSubview:textView]; self.textView = textView; //监听通知 [[NSNotificationCenter self.textView.hasText) { NSLog(@"文字发生改变----%@",self.textView.text); } } -(void)dealloc { //移除通知 [[NSNotificationCenter ) { // 当UITextView的文字发生改变时,UITextView自己会发出一个UITextViewTextDidChangeNotification通知 [[NSNotificationCenter [self.placeholder drawInRect:placeholderRect withAttributes:attributes]; } -(void)dealloc { [[NSNotificationCenter
, error); } dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter } dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter responseObject, error); } dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter task.response, responseObject, error); } dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter self.downloadFileURL error:&fileManagerError]; if (fileManagerError) { [[NSNotificationCenter
addDeviceRotateObserver { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter // do something break; } } } - (void) removeDeviceRotateObserver { [[NSNotificationCenter
关闭友盟自带的弹出框 [UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; [[NSNotificationCenter statisticsViewWillAppear:animated]; [MobClick beginLogPageView:NSStringFromClass([self class])]; [[NSNotificationCenter statisticsViewWillDisappear:animated]; [MobClick endLogPageView:NSStringFromClass([self class])]; [[NSNotificationCenter
timer invalidate]; _timer = nil; } } - (void)observeApplicationActionNotification { [[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter
_audioStream.onCompletion=^(){ [weakSelf nextButtonAction]; }; 三.自己的案例 昨天我遇到一奇葩问题,用得到(NSNotificationCenter 我明明只发送一次通知,却接受到几次通知,ViewController POP返回后,再次push进来,又多收一次通知,意识到可能通知没有移除,我的方法: -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"delectOrGoDownProject" object:nil]; [[NSNotificationCenter