在Objective-C项目中提供一个React组件作为视图控制器,然后将该控制器从React-Native中删除。演示部分很简单:
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];
RCTRootView *rootView =
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
moduleName: @"MyListView"
initialProperties:nil
launchOptions: nil];
UIViewController *vc = [[UIViewController alloc] init];
vc.view = rootView;
[self presentViewController:vc animated:YES completion:nil];
如何向React Native公开Objective-C类:
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(dismiss: (NSString *) name){
NSLog(@"dismiss");
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}
如何在React Native中访问此类:
var ViewController = NativeModules.ViewController;
backTap = () => {
ViewController.dismiss('Blah');
}
我确实在RCT_EXPORT_METHOD (dismiss:)
方法中得到了调用,但是react-native控制器只停留在那里。