我有一个项目,我将本机代码与React Native混合。我像这样显示React视图:
self.reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"DemoPage" initialProperties:initialProperties];
[self.view addSubview:self.reactView];
以上作品和视图正确显示。视图是项目列表。当选择列表项时,它应该响应我的视图控制器,我将推送本机视图控制器。我使用这样的Native模块执行此操作:
RCT_EXPORT_MODULE(NavigationUtils);
RCT_EXPORT_METHOD(navigateWithParameters:(NSString *)parameters)
{
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
我的问题是,在尝试pushViewController:
时,self.navigationController
为nil
,因此推送无效。
presentViewController:
也不起作用。
self.navigationController
或nil
中 viewWillAppear:
不是viewDidAppear:
有谁知道如何从React Native Module推送新的本机视图控制器?
以下是视图层次结构的recursiveDescription的编辑版本。
<UIWindow: 0x102a10970; frame = (0 0; 414 736); autoresize = W+H; tintColor = UIExtendedSRGBColorSpace 0.313725 0.52549 0.764706 1; gestureRecognizers = <NSArray: 0x17405ab20>; layer = <UIWindowLayer: 0x17422ae20>>
| <UIView: 0x102b55ff0; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x17064e550>; layer = <CALayer: 0x170631220>>
| | <UIView: 0x102b6b580; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x170631260>>
| | | <UILayoutContainerView: 0x102b689d0; frame = (0 0; 314 736); hidden = YES; autoresize = RM+H; gestureRecognizers = <NSArray: 0x17064d410>; layer = <CALayer: 0x17062f4e0>>
| | | | <UINavigationTransitionView: 0x102b6ab20; frame = (0 0; 314 736); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x170631100>>
| | | | | <UIViewControllerWrapperView: 0x102b74ad0; frame = (0 0; 314 736); autoresize = W+H; layer = <CALayer: 0x1706350c0>>
| | | | | | <UITableView: 0x10388f400; frame = (0 0; 314 736); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1748569e0>; layer = <CALayer: 0x17482a600>; contentOffset: {0, 0}; contentSize: {314, 630}>
| | | | | | | ...Cells and seperators
| | | | <UINavigationBar: 0x102b68b90; frame = (0 20; 314 44); opaque = NO; autoresize = W; gestureRecognizers = <NSArray: 0x17064db00>; layer = <CALayer: 0x170630c60>>
| | | | | <_UIBarBackground: 0x102b68e40; frame = (0 -20; 314 64); userInteractionEnabled = NO; layer = <CALayer: 0x170630ce0>>
| | | | | | <UIImageView: 0x102b69450; frame = (0 64; 314 0.333333); userInteractionEnabled = NO; layer = <CALayer: 0x170630d20>> - (null)
| | | | | | <UIVisualEffectView: 0x102b69640; frame = (0 0; 314 64); layer = <CALayer: 0x170630d40>>
| | | | | | | <_UIVisualEffectBackdropView: 0x102b69a80; frame = (0 0; 314 64); autoresize = W+H; userInteractionEnabled = NO; layer = <UICABackdropLayer: 0x170630da0>>
| | | | | | | <_UIVisualEffectFilterView: 0x102b6a380; frame = (0 0; 314 64); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x170630f60>>
| | | | | | | <_UIVisualEffectFilterView: 0x102a850b0; frame = (0 0; 314 64); alpha = 0.85; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x17482c3e0>>
| | | | | <UIImageView: 0x102a7e940; frame = (89 3.33333; 136 37.6667); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x174829240>> - (null)
| | | | | <_UINavigationBarBackIndicatorView: 0x102b6a730; frame = (12 11.6667; 13 21); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x170631000>> - Back
| | | <MMDrawerCenterContainerView: 0x102b6b8d0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x1706312c0>>
| | | | <UILayoutContainerView: 0x102aafb40; frame = (0 0; 414 736); autoresize = W+H; gestureRecognizers = <NSArray: 0x170441890>; layer = <CALayer: 0x17422ad60>>
| | | | | <UINavigationTransitionView: 0x102ab02e0; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x17422adc0>>
| | | | | | <UIViewControllerWrapperView: 0x102a828b0; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x174a201a0>>
| | | | | | | <UIView: 0x102b98e20; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x170c209a0>>
| | | | | | | | <RCTRootView: 0x102b9a8d0; frame = (0 64; 414 736); autoresize = W+H; layer = <CALayer: 0x170c221c0>>
| | | | | | | | | ...subviews of RCTRootView
| | | | | <UINavigationBar: 0x102aaf160; frame = (0 20; 414 44); opaque = NO; autoresize = W; gestureRecognizers = <NSArray: 0x174258690>; layer = <CALayer: 0x17422a720>>
| | | | | | <_UIBarBackground: 0x102aaf410; frame = (0 -20; 414 64); userInteractionEnabled = NO; layer = <CALayer: 0x17422ab40>>
| | | | | | | <UIImageView: 0x102a9e1e0; frame = (0 64; 414 0.333333); userInteractionEnabled = NO; layer = <CALayer: 0x17422a9c0>> - (null)
| | | | | | | <UIVisualEffectView: 0x102a9e3d0; frame = (0 0; 414 64); layer = <CALayer: 0x174230c60>>
| | | | | | | | <_UIVisualEffectBackdropView: 0x102a98840; frame = (0 0; 414 64); autoresize = W+H; userInteractionEnabled = NO; layer = <UICABackdropLayer: 0x1742327c0>>
| | | | | | | | <_UIVisualEffectFilterView: 0x102a98ba0; frame = (0 0; 414 64); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x174233560>>
| | | | | | | | <_UIVisualEffectFilterView: 0x102ab1380; frame = (0 0; 414 64); alpha = 0.85; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x174233660>>
| | | | | | <<UINavigationItemView: 0x102ab04c0; frame = (169.333 8; 75.3333 27); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x174422a80>>: item=<<UINavigationItem: 0x1703c6900>: title:'Accounts'> title=Accounts>
| | | | | | | <UILabel: 0x102b1a660; frame = (0 3; 75.3333 21.3333); text = 'Accounts'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x17488cda0>>
| | | | | | <UINavigationButton: 0x102a80b40; frame = (9 6; 48 30); opaque = NO; tintColor = UIExtendedSRGBColorSpace 0.313725 0.52549 0.764706 1; layer = <CALayer: 0x174830340>>
| | | | | | | <UIImageView: 0x102b66cd0; frame = (11 2; 26 26); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x170c20bc0>> - (null)
| | | | | | <_UINavigationBarBackIndicatorView: 0x102ab00f0; frame = (12 11.6667; 13 21); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x17422ad20>> - Back
答案 0 :(得分:0)
您是否尝试过rootViewController提供vc?
UIViewController *rootVC = [[[UIApplication sharedApplication] window] rootViewController];
[rootVC presentViewController:viewController animated:YES completion: nil];
<强>更新强>
关于你的视图层次结构,我可以建议使用一些丑陋的内省来推动:
UIViewController *rootVC = [[[UIApplication sharedApplication] window] rootViewController];
if ([rootVC isKindOfClass:[MMDrawerController class]]) {
MMDrawerController *drawer = (MMDrawerController *)rootVC;
UIViewController *realRootVC = drawer.centerViewController;
if (realRootVC != nil) {
if (realRootVC.navigationController != nil) {
[realRootVC.navigationController presentViewController:viewController animated:YES completion: nil];
} else if ([realRootVC isKindOfClass:[UINavigationController class]]) {
UINavigationController *nv = (UINavigationController *)realRootVC;
[nv presentViewController:viewController animated:YES completion: nil];
}
}
}