Skip to content

Commit

Permalink
通知中心
Browse files Browse the repository at this point in the history
通知中心
  • Loading branch information
BigShow1949 committed Jul 15, 2016
1 parent 5702733 commit e4c8bd8
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 20 deletions.
26 changes: 26 additions & 0 deletions BigShow1949.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
18CAD0D21B998A93001066FF /* snow.png in Resources */ = {isa = PBXBuildFile; fileRef = 18CAD0C81B998A93001066FF /* snow.png */; };
18CAD0D31B998A93001066FF /* YFSnowViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CAD0CA1B998A93001066FF /* YFSnowViewController.m */; };
18CAD0D71B998F92001066FF /* YFEmitterRainCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CAD0D61B998F92001066FF /* YFEmitterRainCell.m */; };
18D0A7FD1D38B9890007B184 /* YFNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = 18D0A7FC1D38B9890007B184 /* YFNotification.m */; };
18D0A8001D38B9EA0007B184 /* YFNotificationCenterVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18D0A7FF1D38B9EA0007B184 /* YFNotificationCenterVC.m */; };
18D0A8031D38BA2E0007B184 /* YFNotificationCenter2VC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18D0A8021D38BA2E0007B184 /* YFNotificationCenter2VC.m */; };
8B0B9AC01B9572000084590D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8B0B9ABF1B9572000084590D /* Images.xcassets */; };
8B0B9AC31B9572000084590D /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8B0B9AC11B9572000084590D /* LaunchScreen.xib */; };
8B0B9ACF1B9572000084590D /* BigShow1949Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B0B9ACE1B9572000084590D /* BigShow1949Tests.m */; };
Expand Down Expand Up @@ -699,6 +702,12 @@
18CAD0CA1B998A93001066FF /* YFSnowViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YFSnowViewController.m; sourceTree = "<group>"; };
18CAD0D51B998F92001066FF /* YFEmitterRainCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YFEmitterRainCell.h; sourceTree = "<group>"; };
18CAD0D61B998F92001066FF /* YFEmitterRainCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YFEmitterRainCell.m; sourceTree = "<group>"; };
18D0A7FB1D38B9890007B184 /* YFNotification.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YFNotification.h; sourceTree = "<group>"; };
18D0A7FC1D38B9890007B184 /* YFNotification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YFNotification.m; sourceTree = "<group>"; };
18D0A7FE1D38B9EA0007B184 /* YFNotificationCenterVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YFNotificationCenterVC.h; sourceTree = "<group>"; };
18D0A7FF1D38B9EA0007B184 /* YFNotificationCenterVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YFNotificationCenterVC.m; sourceTree = "<group>"; };
18D0A8011D38BA2E0007B184 /* YFNotificationCenter2VC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YFNotificationCenter2VC.h; sourceTree = "<group>"; };
18D0A8021D38BA2E0007B184 /* YFNotificationCenter2VC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YFNotificationCenter2VC.m; sourceTree = "<group>"; };
8B0B9AAF1B9572000084590D /* BigShow.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BigShow.app; sourceTree = BUILT_PRODUCTS_DIR; };
8B0B9AB31B9572000084590D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8B0B9ABF1B9572000084590D /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1857,6 +1866,7 @@
children = (
18CAD0301B95FD88001066FF /* YFKnowledgeViewController.h */,
18CAD0311B95FD88001066FF /* YFKnowledgeViewController.m */,
18D0A7F71D38B9000007B184 /* NotificationCenter */,
181F28921D2CEE1700FC2637 /* RunTime */,
B86B4A871CE1DAD900AD54AB /* GuideView */,
B80981DE1CBE1D9E0030BF28 /* ResponderChain */,
Expand Down Expand Up @@ -2082,6 +2092,19 @@
path = RainCell;
sourceTree = "<group>";
};
18D0A7F71D38B9000007B184 /* NotificationCenter */ = {
isa = PBXGroup;
children = (
18D0A7FB1D38B9890007B184 /* YFNotification.h */,
18D0A7FC1D38B9890007B184 /* YFNotification.m */,
18D0A7FE1D38B9EA0007B184 /* YFNotificationCenterVC.h */,
18D0A7FF1D38B9EA0007B184 /* YFNotificationCenterVC.m */,
18D0A8011D38BA2E0007B184 /* YFNotificationCenter2VC.h */,
18D0A8021D38BA2E0007B184 /* YFNotificationCenter2VC.m */,
);
path = NotificationCenter;
sourceTree = "<group>";
};
8B0B9AA61B9572000084590D = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4227,6 +4250,7 @@
B88022FA1CAE279D002C0021 /* UIImage+EXtension.m in Sources */,
B82BF6BD1CC9CAC00041F53C /* DataEditView.m in Sources */,
18C760D41B9ACC960038B2EE /* SYArenaViewController.m in Sources */,
18D0A7FD1D38B9890007B184 /* YFNotification.m in Sources */,
B87445E81CACBD48009F274E /* YiRefreshFooter.m in Sources */,
18C75C711B9AA77B0038B2EE /* SYEmotionTool.m in Sources */,
B80981E61CBE1E430030BF28 /* UIViewA.m in Sources */,
Expand Down Expand Up @@ -4419,7 +4443,9 @@
18CAD07C1B9977E3001066FF /* YFAnimationVC02.m in Sources */,
B87445EE1CACBD48009F274E /* YiCustomTableViewCell.m in Sources */,
184B38321D250C57005CD7F1 /* DataSource2.m in Sources */,
18D0A8031D38BA2E0007B184 /* YFNotificationCenter2VC.m in Sources */,
B8E9F0361CEAF11E00E7E668 /* UIAlertView+AFNetworking.m in Sources */,
18D0A8001D38B9EA0007B184 /* YFNotificationCenterVC.m in Sources */,
B8A57EDF1CC50183003ED5EF /* DataListController.m in Sources */,
B891DAF11CA11ABB007F69AF /* ViewController+MASAdditions.m in Sources */,
B891DAEA1CA11ABB007F69AF /* MASConstraintMaker.m in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,4 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BigShow1949/Classes/KnowledgePoint/RunTime/ExchangeMethod/UIImage+Image.m"
timestampString = "489490107.224515"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "54"
endingLineNumber = "54"
landmarkName = "+imageWithName:"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// YFNotification.h
// BigShow1949
//
// Created by 杨帆 on 16/7/15.
// Copyright © 2016年 BigShowCompany. All rights reserved.
//

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN // 在这两个宏之间的代码,所有简单指针对象都被假定为nonnull,因此我们只需要去指定那些nullable的指针
// __nullable表示对象可以是NULL或nil,而__nonnull表示对象不应该为空。当我们不遵循这一规则时,编译器就会给出警告。

/**************** YFNotification ****************/
@interface YFNotification : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) id observer;
@property (nonatomic, strong) id object;
@property (nonatomic, assign) SEL selector;

+ (YFNotification *)notificationWithObserver:(id)observer selector:(SEL)selector name:(nullable NSString *)aName object:(nullable id)anObject;

@end


/**************** YFNotificationCenter ****************/

@interface YFNotificationCenter : NSObject


+ (YFNotificationCenter *)defaultCenter;

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

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

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


@end


NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// YFNotification.m
// BigShow1949
//
// Created by 杨帆 on 16/7/15.
// Copyright © 2016年 BigShowCompany. All rights reserved.
//

#import "YFNotification.h"
/**************** YFNotification ****************/
@implementation YFNotification
+ (YFNotification *)notificationWithObserver:(id)observer selector:(SEL)selector name:(nullable NSString *)aName object:(nullable id)anObject {

YFNotification *notification = [[YFNotification alloc] init];
notification.observer = observer;
notification.name = aName;
notification.object = anObject;
notification.selector = selector;
return notification;
}
@end



/**************** YFNotificationCenter ****************/

static YFNotificationCenter *_instance =nil;

@interface YFNotificationCenter ()
@property (nonatomic, strong) NSMutableArray *notiArr;
@end

@implementation YFNotificationCenter


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

YFNotification *noti = [YFNotification notificationWithObserver:observer selector:aSelector name:aName object:anObject];
[self.notiArr addObject:noti];
}

- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject {
for (YFNotification *noti in _notiArr) {
if ([noti.name isEqualToString:aName]) {
noti.object = anObject;
[noti.observer performSelector:noti.selector withObject:noti];
}
}
}

- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject {
for (YFNotification *noti in _notiArr) {
if ([noti.name isEqualToString:aName] && [noti.name isEqual:observer]) {
[self.notiArr removeObject:noti];
}
}
}

- (void)removeObserver:(id)observer {
for (YFNotification *noti in _notiArr) {
if ([noti.object isEqual:observer]) {
[self.notiArr removeObject:noti];
}
}
}




#pragma mark - 单例
+ (id)allocWithZone:(NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}

+ (YFNotificationCenter *)defaultCenter {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}

#pragma mark - 懒加载
- (NSMutableArray *)notiArr{
if (!_notiArr) {
_notiArr =[NSMutableArray array];
}
return _notiArr;
}
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// YFNotificationCenter2VC.h
// BigShow1949
//
// Created by 杨帆 on 16/7/15.
// Copyright © 2016年 BigShowCompany. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YFNotificationCenter2VC : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// YFNotificationCenter2VC.m
// BigShow1949
//
// Created by 杨帆 on 16/7/15.
// Copyright © 2016年 BigShowCompany. All rights reserved.
//

#import "YFNotificationCenter2VC.h"
#import "YFNotification.h"

@implementation YFNotificationCenter2VC

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

UIButton *redButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[redButton setTitle:@"点我啊" forState:UIControlStateNormal];
[redButton addTarget:self action:@selector(redButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:redButton];
}


- (void)redButtonClick:(UIButton *)btn {

[[NSNotificationCenter defaultCenter] postNotificationName:@"NSNotificationCenter" object:nil];
[[YFNotificationCenter defaultCenter] postNotificationName:@"YFNotificationCenter" object:nil];
[self.navigationController popViewControllerAnimated:YES];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// YFNotificationCenterVC.h
// BigShow1949
//
// Created by 杨帆 on 16/7/15.
// Copyright © 2016年 BigShowCompany. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YFNotificationCenterVC : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// YFNotificationCenterVC.m
// BigShow1949
//
// Created by 杨帆 on 16/7/15.
// Copyright © 2016年 BigShowCompany. All rights reserved.
//

#import "YFNotificationCenterVC.h"
#import "YFNotificationCenter2VC.h"
#import "YFNotification.h"
@implementation YFNotificationCenterVC

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"right" style:UIBarButtonItemStylePlain target:self action:@selector(right)];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NSTest:) name:@"NSNotificationCenter" object:nil];

[[YFNotificationCenter defaultCenter] addObserver:self selector:@selector(YFTest:) name:@"YFNotificationCenter" object:nil];
}

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];
// [[NSNotificationCenter defaultCenter] removeObserver:<#(nonnull id)#>];
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:nil context:nil];

}

- (void)dealloc {

[[YFNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];

[[YFNotificationCenter defaultCenter] removeObserver:self];
}

- (void)NSTest:(YFNotification *)noti {

UIButton *redButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[redButton setTitle:@"NS" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
[self.view addSubview:redButton];
}

- (void)YFTest:(YFNotification *)noti {
UIButton *redButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
[redButton setTitle:@"YF" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor blueColor];
[self.view addSubview:redButton];
}



- (void)right {

YFNotificationCenter2VC *vc = [[YFNotificationCenter2VC alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ - (void)viewDidLoad {
@"生命周期",
@"响应者链条",
@"引导页",
@"运行时"];
@"运行时",
@"通知中心"];

self.classNames = @[@"YFKnowledgeVC02",
@"YFSearchController",
@"YFMasonryDemoViewController_UIStoryboard",
@"YFLifeCycleViewController",
@"YFResponderChainViewController",
@"YFGuideViewController",
@"YFRunTimeViewController"];
@"YFRunTimeViewController",
@"YFNotificationCenterVC"];
}


Expand Down

0 comments on commit e4c8bd8

Please sign in to comment.