在 React Native 中,有没有办法在不使用 Native Modules 的情况下发送 NSNotifications? [英] In React Native, is there a way to send NSNotifications without using Native Modules?
本文介绍了在 React Native 中,有没有办法在不使用 Native Modules 的情况下发送 NSNotifications?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找用 Objective-C 编写不需要回调的简单 UI 处理程序代码的方法.
I'm looking for ways of writing simple UI handler code in Objective-C that doesn't require a call-back.
推荐答案
据我所知没有这样的模块.你可以自己做一个.这很简单.
As far as I know there is no such Module. You can make your own one. It's very simple.
代码:
// NotificationManager.h
#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
@interface NotificationManager : NSObject <RCTBridgeModule>
@end
// NotificationManager.m
#import "NotificationManager.h"
@implementation NotificationManager
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(postNotification:(NSString *)name) {
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:nil];
}
@end
现在您可以通过 JavaScript 发送简单的通知
Now you can simple post notification from JavaScript
var NotificationManager = require('react-native').NativeModules.NotificationManager;
NotificationManager.postNotification("TestEvent")
这篇关于在 React Native 中,有没有办法在不使用 Native Modules 的情况下发送 NSNotifications?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文