[英]React Native call iOS ViewController
How to call native iOS viewController' code when in RN?在 RN 中如何调用本机 iOS viewController 的代码?
Like use RN to push to a native iOS viewController and then let the native code do the job.就像使用 RN 推送到本机 iOS viewController 然后让本机代码完成这项工作一样。
You need to use Export Method .您需要使用导出方法。
AppDelegate.h AppDelegate.h
#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeModule>
@property (nonatomic, strong) UIWindow *window;
@end
AppDelegate.m AppDelegate.m
@implementation AppDelegate
RCT_EXPORT_MODULE()
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//NO Change Here
}
RCT_EXPORT_METHOD(pushVC:(NSString *)vcName){
Class ctrlClass = NSClassFromString(vcName);
UIViewController *newVc = [[ctrlClass alloc] initWithNibName: vcName bundle: nil];
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:newVc animated:YES completion:nil];
}
You can call above response in javascript like this:您可以像这样在 javascript 中调用上面的响应:
import { NativeModules } from 'react-native'
NativeModules.AppDelegate.pushVC('viewControllerNameHere')
But I highly recommend to make with external file to bridge like documentation explain here:https://facebook.github.io/react-native/docs/native-modules-ios但我强烈建议使用外部文件进行桥接,如此处的文档解释:https ://facebook.github.io/react-native/docs/native-modules-ios
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.