React-native: integrate react-native component with UINavigationController did not work

Created on 10 Oct 2015  Â·  5Comments  Â·  Source: facebook/react-native

I want combine react-native component with an existing app, and use an react-native component as a view of a viewcontroller. one problem I met is that how to integrate react-native component with UINavigationController. In react-native, I can use the NavigatorIOS or Navigator component, but now that I use react-native component as the view part, I don't want mixin a navigator in the component. so I think if I can solve the problem.

In AppDelegate.m file:

  ...
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *PageOneVC = [[PageOne alloc] init];
  UINavigationController *rootController = [[UINavigationController alloc] initWithRootViewController: PageOneVC];
  ...

and then I write a custom module to let js call objective-c method:

#import "POM.h"
#import "AppDelegate.h"
#import "PageTwo.h"

@implementation POM
 RCT_EXPORT_MODULE();
 RCT_EXPORT_METHOD(gopagetwo: (NSString *) gopagetwo) {
  NSLog(@"123");
  AppDelegate *share = (AppDelegate *)[UIApplication sharedApplication].delegate;
  UINavigationController *nav = (UINavigationController *) share.window.rootViewController;
  PageTwo *pagetwo = [[PageTwo alloc] init];
  [nav pushViewController:pagetwo animated:TRUE];
}
@end

and in my react-native component, I require this module and call the gopagetwo method.

When I run the app, there is no error or warning, and the gopagetwo method is called indeed because the NSLog method did work, but the UINavigationController's pushViewController method doesn't work.

did I do something wrong? any suggestion could help. thx.

Locked

Most helpful comment

I solve this problem by run viewcontroller switch operation on main thread:

@implementation POM
 RCT_EXPORT_MODULE();

 - (dispatch_queue_t)methodQueue
  {
   return dispatch_get_main_queue();
 }

 RCT_EXPORT_METHOD(gopagetwo: (NSString *) gopagetwo) {
  NSLog(@"123");
  AppDelegate *share = (AppDelegate *)[UIApplication sharedApplication].delegate;
  UINavigationController *nav = (UINavigationController *) share.window.rootViewController;
  PageTwo *pagetwo = [[PageTwo alloc] init];
  [nav pushViewController:pagetwo animated:TRUE];
  //[share.emitter emit:@"gopagetwo"];
}
@end

lol !

All 5 comments

It's cool

I solve this problem by run viewcontroller switch operation on main thread:

@implementation POM
 RCT_EXPORT_MODULE();

 - (dispatch_queue_t)methodQueue
  {
   return dispatch_get_main_queue();
 }

 RCT_EXPORT_METHOD(gopagetwo: (NSString *) gopagetwo) {
  NSLog(@"123");
  AppDelegate *share = (AppDelegate *)[UIApplication sharedApplication].delegate;
  UINavigationController *nav = (UINavigationController *) share.window.rootViewController;
  PageTwo *pagetwo = [[PageTwo alloc] init];
  [nav pushViewController:pagetwo animated:TRUE];
  //[share.emitter emit:@"gopagetwo"];
}
@end

lol !

still not work in my app :(

react was error display ...

//
// PrinterManager.m
// fastui
//
// Created by Xhacker on 16/1/16.
// Copyright © 2016年 Facebook. All rights reserved.
//

import "PrinterManager.h"

import "RCTConvert.h"

import "RCTBridge.h"

import "RCTEventDispatcher.h"

import

import "OCDirector.h"

import "HJPrinterDemoViewController.h"

@implementation PrinterManager

RCT_EXPORT_MODULE();

  • (dispatch_queue_t)methodQueue
    {
    return dispatch_get_main_queue();
    }

RCT_EXPORT_METHOD(gotoPrinterSetting:(RCTResponseSenderBlock)callback)
{

RCTLogInfo(@"gotoPringer");

UINavigationController *nav =  (UINavigationController*)[UIApplication sharedApplication].delegate.window.rootViewController;

HJPrinterDemoViewController *loginVC = [[HJPrinterDemoViewController alloc] init];

[nav pushViewController:loginVC animated:TRUE];

}

@end

2016-01-16 16:08:34.315 [trace][tid:main][PrinterManager.m:33] gotoPringer
2016-01-16 16:08:34.315 fastui[786:221044] -[UIViewController pushViewController:animated:]: unrecognized selector sent to instance 0x15f63c630
2016-01-16 16:08:34.316 [error][tid:main] Exception thrown while invoking gotoPrinterSetting on target PrinterManager with params (
17
): -[UIViewController pushViewController:animated:]: unrecognized selector sent to instance 0x15f63c630

@jabez128 Answer will work to push to next view controller. And we have to do the operation on the main queue otherwise, it will show some error.

@jabez128 Just wanted to let you know that I was stuck on this for more than a week and your answer saved my sanity. Thanks! I owe you 20+ beers

Was this page helpful?
0 / 5 - 0 ratings