如何从react-native代码转到特定的本机视图控制器?
我是反应原生的新手。我在现有的swift应用程序中添加了一个反应原生的功能。我从我的原生视图控制器中提供了 RCTRootview
。当用户点击后退按钮时,我必须转到以swift编写的主页。如何从react-native到本机应用程序代码进行通信。有人可以帮我这个场景。从最近2天开始,我就陷入了困境。在此先感谢。
I'm a newbie in react-native. I'm adding one feature in react-native to an existing swift application. I presented the RCTRootview
from my native view controller. From there when user clicks on back button I have to go to Homepage which is written in swift. How do I communicate from react-native to native application code. Can someone please help me with this scenerio. I stuck at this point from last 2 days. Thanks in advance.
是的。我找到了答案。使用 RCTBridgeModule 可以实现这一切。这段代码说明了如何做到这一点。
Yes. I found out the answer for this. It is all possible with RCTBridgeModule. This piece of code illustrate how to do that.
#import "CalendarManager.h"
#import <React/RCTLog.h>
@implementation CalendarManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(@"Pretending to create an event %@ at %@", name, location);
}
现在,您可以从JavaScript文件中调用以下方法:
Now, from your JavaScript file you can call the method like this:
import {NativeModules} from 'react-native';
var CalendarManager = NativeModules.CalendarManager;
CalendarManager.addEvent('Birthday Party', '4 Privet Drive, Surrey');
请按照以下官方链接关于这个概念以及如何做到这一点。
Please follow the below official link about the concept and how to do it.