English | 中文
XNGNotificationProxy is a replacement of custom NSNotification.
You followed a user in VC_U, so the follow buttons in VC_A and VC_B should update state.
// VC_U.m
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserDidFollowUser"
object:nil
userInfo:@{@"isFollowed": @(YES), @"userID": @(123456)}];
// VC_A.m or VC_B.m
[[NSNotificationCenter defaultCenter] addObserverForName:@"UserDidFollowUser"
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
BOOL isFollowed = [note.userInfo[@"isFollowed"] boolValue];
NSNumber *userID = note.userInfo[@"userID"];
}];
There are a lot of magic strings. We may define some global constant strings to solve this, but we can never know what parameters are in the userInfo intuitively.
You need create a category for XNGNotificationProxy that conforms a custom protocol first.
// XNGNotificationProxy+Protocol.h
@protocol UserActionProtocol <NSObject>
@optional
- (void)userDidFollow:(BOOL)isFollowed userID:(NSNumber *)userID;
@end
@interface XNGNotificationProxy (Protocol) <UserActionProtocol>
@end
then
// VC_U.m
[[XNGNotificationProxy sharedProxy] userDidFollow:YES userID:@(123456)];
// VC_A.m
@interface VC_A () <UserActionProtocol>
@end
// -viewDidLoad or somewhere else
[[XNGNotificationProxy sharedProxy] registerProtocol:@protocol(UserActionProtocol) forObject:self];
//
- (void)userDidFollow:(BOOL)isFollowed userID:(NSNumber *)userID {
// do something
}
iOS 7.0 or later.
There are two ways to use XNGNotificationProxy in your project:
- using CocoaPods
- drag
XNGNotificationProxy
folder into your project
XNGNotificationProxy is released under the MIT license. See LICENSE for details.