Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The main view controller's viewDidLoad looks like this:

-(void)viewDidLoad {
    ...
    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName: @"ListingsView" launchOptions:nil];
    self.view = rootView;
}

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];
        });

}

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!

like image 966
gogi Avatar asked Oct 31 '22 07:10

gogi


1 Answers

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];
    });
}

In this example my controller, which i needed, has a place this window>rootViewController>authViewController

UPD

this i found some solution how we can get current ViewController

like image 115
Bimawa Avatar answered Nov 03 '22 00:11

Bimawa