【发布时间】:2015-06-18 07:23:39
【问题描述】:
我们正在将 react native 添加到现有应用程序中,并且无法将原生视图控制器推送到另一个包含 react native 视图的原生视图控制器之上。
主视图控制器的 viewDidLoad 如下所示:
-(void)viewDidLoad {
...
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: @"ListingsView" launchOptions:nil];
self.view = rootView;
}
react 视图是一个 ListView,其中 TouchableHighlight 的 onPress 被导出到同一视图控制器上的此方法:
RCT_EXPORT_METHOD(productClicked:(NSDictionary *)productDict)
{
dispatch_async(dispatch_get_main_queue(),
^{
SNKProduct *product = [[SNKProduct alloc] initWithDictionary:productDict];
SNKProductViewController *vc = [[SNKProductViewController alloc] initWithProduct:product];
[self.navigationController pushViewController:vc animated:YES];
});
}
该方法确实被调用,但 SNKProductViewController 从未被推送到屏幕上(没有日志消息)。我还尝试以模态方式呈现视图控制器,并收到以下控制台消息:
Warning: Attempt to present <SNKProductViewController: 0x7feadf247d10> on <SNKProductsViewController: 0x7feada5e2a20> whose view is not in the window hierarchy!
任何帮助将不胜感激,非常感谢!
【问题讨论】:
-
既然你使用self来推送视图控制器,你能确认RCT_EXPORT_METHOD和viewDidLoad在同一个类(即你的MainViewController)吗?否则获取 topController 并推送 SNKProductViewController。 UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topController.presentedViewController) { topController = topController.presentedViewController; } [topController pushViewController:vc 动画:YES];
-
不相关,但设置视图的正确方法是使用 -(void)loadView 方法而不是 viewDidLoad。
标签: ios react-native