通知

作者: Jean_Lina | 来源:发表于2021-06-09 10:24 被阅读0次
    通知的定义
    //定义通知名
    static NSString * const AFNSURLSessionTaskDidResumeNotification  = @"com.alamofire.networking.nsurlsessiontask.resume";
    static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofire.networking.nsurlsessiontask.suspend";
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:AFNSURLSessionTaskDidResumeNotification object:self];
    
    //注册通知监听者
    - (void)addNotificationObserverForTask:(NSURLSessionTask *)task {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidResume:) name:AFNSURLSessionTaskDidResumeNotification object:task];
    }
    
    //移除通知的监听者(不做移除操作,不会产生崩溃和循环引用)
    - (void)removeNotificationObserverForTask:(NSURLSessionTask *)task {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:AFNSURLSessionTaskDidSuspendNotification object:task];
    }
    
    
    KVO的定义:
    
    //注册监听者
    [self.courseTableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    //实现监听方法 
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
        if ([keyPath isEqualToString:@"contentOffset"]) {
            NSLog(@"object = %@", object);
        }
    }
    //移除监听者(不移除程序崩溃)
    - (void)dealloc
    {
        [self.courseTableView removeObserver:self forKeyPath:@"contentOffset"];
    }
    
    
    UIControl和UIView的关系:
    UITableView : UIScrollView
    UIScrollView : UIView
    UIView : UIResponder
    UIResponder : NSObject
    
    UIButton : UIControl
    UIControl : UIView
    UIView : UIResponder
    UIResponder : NSObject
    

    相关文章

      网友评论

          本文标题:通知

          本文链接:https://www.haomeiwen.com/subject/hvhzsltx.html