我的测试很简单。
在didFinishLaunchingWithOptions中,我创建了一个UINavigationController和一个包含RCTRootView的UIViewController。然后将UIViewController推送到UINavigationController的堆栈。
#import "AppDelegate.h"
#import "RCTRootView.h"
UINavigationController* theNavi = nil;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"TouchableFeedbackEvents"
launchOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
UINavigationController* nav = theNavi = [[UINavigationController alloc] init];
[nav pushViewController:rootViewController animated:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
@end
我写了一个可以加载另一个RCTRootView的桥接器,并将新的UIViewController推送到导航控制器。
#import "ReactNativeBridger.h"
#import "RCTRootView.h"
extern UINavigationController* theNavi;
@implementation ReactNativeBridger
- (void)startReactApp:(NSString*)componentName title:(NSString*)title
{
RCT_EXPORT();
NSURL *jsCodeLocation;
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:componentName
launchOptions:@{}];
UIViewController* vc = [[UIViewController alloc] init];
vc.view = rootView;
vc.title = title;
dispatch_async(dispatch_get_main_queue(), ^{
[theNavi pushViewController:vc animated:YES];
});
}
@end
js只做一件事。按下,脚本将调用startReactApp方法并推送一个新的RCTRootView。
'use strict';
var React = require('react-native');
var {
StyleSheet,
AppRegistry,
Text,
TouchableOpacity,
View,
} = React;
var ReactNativeBridger = require('NativeModules').ReactNativeBridger;
var TouchableFeedbackEvents = React.createClass({
getInitialState: function() {return {};},
render: function() {
return (
<View style={{top: 100}}>
<TouchableOpacity
style={styles.wrapper}
onPress={() => this._press('press')}>
<Text style={styles.button}>Press Me</Text>
</TouchableOpacity>
</View>
);
},
_press: function(eventName) {
ReactNativeBridger.startReactApp('TouchableFeedbackEvents', 'AAAA');}
});
var styles = StyleSheet.create({
row: {
justifyContent: 'center',
flexDirection: 'row',
},
button: {
color: '#007AFF',
},
wrapper: {
borderRadius: 8,
},
});
AppRegistry.registerComponent('TouchableFeedbackEvents', () => TouchableFeedbackEvents);
第一个视图效果很好。按下按钮,将推送另一个视图。但是当我在第二个视图上单击相同的按钮时,没有任何反应。它无法回应我的紧急事件。