无法从本机视图控制器推送本机视图控制器 [英] Can't push a native view controller from a react-native click
问题描述
我们正在为现有应用添加本机响应,并且在将本机视图控制器置于另一个包含反应原生视图的本机视图控制器之上时遇到问题。
We're adding react native to an existing app and are having trouble pushing a native view controller on top of another native view controller housing a react native view.
主视图控制器的viewDidLoad如下所示:
The main view controller's viewDidLoad looks like this:
-(void)viewDidLoad {
...
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: @"ListingsView" launchOptions:nil];
self.view = rootView;
}
反应视图是一个ListView,其中TouchableHighlight的onPress被导出到此方法相同的视图控制器:
The react view is a ListView where the TouchableHighlight's onPress is exported to this method on the same view controller:
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永远不会被推送到屏幕上(没有日志消息)。我也试过模态呈现视图控制器,并收到此控制台消息:
The method is definitely called, but the SNKProductViewController is never pushed onto the screen (no log messages). I also tried modally presenting the view controller, and am getting this console message:
Warning: Attempt to present <SNKProductViewController: 0x7feadf247d10> on <SNKProductsViewController: 0x7feada5e2a20> whose view is not in the window hierarchy!
任何帮助都将不胜感激,非常感谢!
Any help would be appreciated, thanks much!
推荐答案
通过Reac-Native调用 RCT_EXPORT_MODULE
宏后,这将创建此模块的新实例。并且在调用 RCT_EXPORT_METHOD
方法之后,此方法从另一个模块实例调用。为了解决这个问题,我发现只有一个解决方案。在js中调用我的ViewController在Hierarchy控制器中调用方法的地方:
After call RCT_EXPORT_MODULE
macros by Reac-Native this created new instance of this module. And after call RCT_EXPORT_METHOD
method this method called from another instance of module. To solve this problem, I found only one solution yet. in called method place from js search my ViewController in Hierarchy controllers:
RCT_EXPORT_MODULE(AuthController);
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
- (void)loadView {
self.view = [[RCTRootView alloc] initWithBundleURL:[[BCEConfigs sharedInstance] getBundleOfReactResources]
moduleName:@"AuthorizationView"
launchOptions:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
RCT_EXPORT_METHOD(a_registrationButton) {
[self presentRegistrationViewController];
}
- (void)presentRegistrationViewController {
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *mainViewController = keyWindow.rootViewController.childViewControllers[0];
BCERegistrationViewController *bceRegistrationViewController = [BCERegistrationViewController new];
dispatch_async(dispatch_get_main_queue(), ^{
[mainViewController presentViewController:bceRegistrationViewController animated:YES completion:nil];
});
}
在这个例子中,我需要的控制器有一个这个<$的地方c $ c> window> rootViewController> authViewController
In this example my controller, which i needed, has a place this window>rootViewController>authViewController
UPD
我发现一些解决方案我们如何获得当前的ViewController
this i found some solution how we can get current ViewController
这篇关于无法从本机视图控制器推送本机视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!