React Native调用iOS ViewController

时间:2016-03-10 03:21:26

标签: ios react-native

如何在RN中调用本机iOS viewController'代码?

比如使用RN推送到本机iOS viewController然后让本机代码完成工作。

2 个答案:

答案 0 :(得分:2)

您需要使用导出方法。

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];
    }

答案 1 :(得分:0)

您可以像这样在javascript中调用上述响应:

import { NativeModules } from 'react-native'
NativeModules.AppDelegate.pushVC('viewControllerNameHere')

但是我强烈建议使用外部文件来桥接类似文档的说明,在这里: https://facebook.github.io/react-native/docs/native-modules-ios