forked from BigShow1949/BigShow1949
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
295 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+13.2 KB
(110%)
...xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
BigShow1949/Classes/KnowledgePoint/NotificationCenter/YFNotification.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
95 changes: 95 additions & 0 deletions
95
BigShow1949/Classes/KnowledgePoint/NotificationCenter/YFNotification.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
BigShow1949/Classes/KnowledgePoint/NotificationCenter/YFNotificationCenter2VC.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
34 changes: 34 additions & 0 deletions
34
BigShow1949/Classes/KnowledgePoint/NotificationCenter/YFNotificationCenter2VC.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
BigShow1949/Classes/KnowledgePoint/NotificationCenter/YFNotificationCenterVC.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
66 changes: 66 additions & 0 deletions
66
BigShow1949/Classes/KnowledgePoint/NotificationCenter/YFNotificationCenterVC.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters