iOS项目中巧用通知
2023-05-09 本文已影响0人
江湖闹士
项目中经常会用到通知传值,如何将通知简化合并到极致,且看下文
1、创建工具类用于发送通知
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SJObjectTool : NSObject
+ (void)SJPostNotificationClass:(Class)className event:(NSInteger)event param:(nullable NSDictionary *)param;
@end
NS_ASSUME_NONNULL_END
#import "SJObjectTool.h"
@implementation SJObjectTool
//className:类名,用于区分通知的对象
//event:事件类型,用于区分对象收到的事件类型
//param:传的值
+ (void)SJPostNotificationClass:(Class)className event:(NSInteger)event param:(nullable NSDictionary *)param
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@(1) forKey:@"_notify_event_type"];
[dict setValue:@(event) forKey:@"_notify_event_event"];
[dict setObject:param forKey:@"_notify_event_param"];
//SJClassNameNotifi:宏定义,根据类名加上前缀来定义一个新的通知name
[[NSNotificationCenter defaultCenter] postNotificationName:SJClassNameNotifi(className) object:nil userInfo:dict];
}
@end
2、宏定义如下(写在自定义的宏定义类中,头文件可写在pch文件中方便使用)
#define SJClassNameNotifi(cls) [@"_notify_event_class_" stringByAppendingString:NSStringFromClass(cls)]
//使用此宏定义来发送通知
#define SJ_NOTIFICATION_OBJ(cls,e,dic) [SJObjectTool SJPostNotificationClass:cls event:e param:dic]
3、收通知操作封装(由于我们所有的类最终都继承自NSObject,所以创建NSObject的分类SJNotifiEvent)
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSObject (SJNotifiEvent)
- (void)SJAddNotifiCationEvent;
- (void)SJRemoveNotificationEvent;
- (void)SJNotificationEvent:(NSInteger)event param:(NSDictionary *)param;
@end
NS_ASSUME_NONNULL_END
#import "NSObject+SJNotifiEvent.h"
@implementation NSObject (SJNotifiEvent)
- (void)SJAddNotifiCationEvent{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__SJ_notifyEvent:) name:SJClassNameNotifi(self.class) object:nil];
}
- (void)SJRemoveNotificationEvent{
[[NSNotificationCenter defaultCenter] removeObserver:self name:SJClassNameNotifi(self.class) object:nil];
}
- (void)__SJ_notifyEvent:(NSNotification *)notify
{
NSDictionary *dict = notify.userInfo;
if([dict isKindOfClass:[NSDictionary class]] && [[dict objectForKey:@"_notify_event_type"] intValue] == 1){
if([self respondsToSelector:@selector(SJNotificationEvent:param:)]){
NSInteger event = [[dict objectForKey:@"_notify_event_event"] integerValue];
NSDictionary *param = dict[@"_notify_event_param"];
void (*SJ_msgsend)(id, SEL, NSInteger , NSDictionary *) = (void (*)(id, SEL, NSInteger , NSDictionary *))objc_msgSend;
SJ_msgsend(self, @selector(SJNotificationEvent:param:), event, param);
}
}
}
4、在页面中使用
比如有两个页面 AViewController、BViewcontroller。AViewController发通知,BViewcontroller接收
//AViewController类的发送通知处 这里宏定义的参数在上面解释过,中间的SJActionEventSendMessage是event我们随后会做枚举处理,见下文
SJ_NOTIFICATION_OBJ(BViewcontroller.class, SJActionEventSendMessage, @{@"param1":@(1314),@"param2":@"520"});
//BViewcontroller类中
//1、首先在viewDidLoad中添加方法
- (void)viewDidLoad {
[super viewDidLoad];
// 添加接收通知(NSObject分类中的方法)
[self EMOAddNotifiCationEvent];
}
/// action 事件
/// @param event action 类型
/// @param param action 参数
- (void)EMONotificationEvent:(NSInteger)event param:(NSDictionary *)param{
// NSLog(@"收取通知回调___%ld",event);
if (event == SJActionEventSendMessage){
//事件处理
NSNumber *param1 = param[@"param1"];
NSString *param2 = param[@"param2"];
}
}
//不要忘记在dealloc中移除通知
- (void)dealloc{
[self SJRemoveNotificationEvent];
}
5、为了方便事件类型的管理,我们把event作为枚举处理
我们可以自定义一个专门放枚举的SJEnums_h类,只需要.h文件即可,所以建 Header File 文件,创建枚举。头文件放到pch中
#ifndef SJEnums_h
#define SJEnums_h
// action 点击类型
typedef NS_ENUM(NSInteger, SJActionEventType) {
SJActionEventSendMessage = 0, // 发送消息通知
SJActionEventReceiveMessage, // 接收消息通知
}
#endif
通过上述操作,有以下好处:
1、通知通过宏定义在操作,简便快捷
2、不用纠结通知的名字,直接谁接收通知就用谁的类名
3、通过类名区分接收对象,通过event区分事件,清晰明了
4、event通过枚举来管理,方便扩展和维护