[英]React Native call iOS ViewController
在 RN 中如何调用本机 iOS viewController 的代码?
就像使用 RN 推送到本机 iOS viewController 然后让本机代码完成这项工作一样。
您需要使用导出方法。
AppDelegate.h
#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeModule>
@property (nonatomic, strong) UIWindow *window;
@end
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];
}
您可以像这样在 javascript 中调用上面的响应:
import { NativeModules } from 'react-native'
NativeModules.AppDelegate.pushVC('viewControllerNameHere')
但我强烈建议使用外部文件进行桥接,如此处的文档解释:https ://facebook.github.io/react-native/docs/native-modules-ios
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.