iOS反应本机-导航不';不能使用本机代码 我正试图使多个框架在IOS环境中协同工作,而在尝试使用由原生Read(RN)触发的本机方法时,我被卡住了。

iOS反应本机-导航不';不能使用本机代码 我正试图使多个框架在IOS环境中协同工作,而在尝试使用由原生Read(RN)触发的本机方法时,我被卡住了。,ios,objective-c,react-native,Ios,Objective C,React Native,尝试了多种方法后(其中一种方法是公开rootViewController并更改触发方法中的UIViewController),即使调用该方法,我也无法更改视图控制器 我将在下面留下一些我尝试过的代码,希望能更好地解释我正在尝试做什么 免责声明:我是obj-c的初学者,也是iOS开发的完整noob-随学随用 这是我的AppDelegate实现: @implementation AppDelegate - (BOOL)application:(UIApplication *)application

尝试了多种方法后(其中一种方法是公开
rootViewController
并更改触发方法中的
UIViewController
),即使调用该方法,我也无法更改视图控制器

我将在下面留下一些我尝试过的代码,希望能更好地解释我正在尝试做什么

免责声明:我是obj-c的初学者,也是iOS开发的完整noob-随学随用

这是我的
AppDelegate
实现:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [super applicationDidFinishLaunching:application];  

  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"SomethingMobile"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  UIViewController *rootViewController = [[UIViewController alloc] init];
  rootViewController.view = rootView;


  self.navigationController = [[UINavigationController alloc] initWithRootViewController:   rootViewController];
  [self.window setRootViewController:self.navigationController];

//--- style the UINavigationController
  self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
  self.navigationController.navigationBar.topItem.title = @"Home";

  return YES;
}

-(void) changeView {
 // SimpleViewController is a valid UIViewController that I tested separately and it works
  [self.navigationController pushViewController:[[SimpleViewController alloc] init] animated:YES];
  self.navigationController.navigationBar.topItem.title = @"OF";
  printf("Got here");
}

@end
- (dispatch_queue_t)methodQueue
{
  return dispatch_get_main_queue();
}
然后我有一个类作为RN和本机代码之间的桥梁:

@implementation OFStarter

// The React Native bridge needs to know our module
RCT_EXPORT_MODULE()

- (NSDictionary *)constantsToExport {
  return @{@"greeting": @"Welcome to native"};
}

RCT_EXPORT_METHOD(squareMe:(int)number:(RCTResponseSenderBlock)callback) {

  AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [delegate changeView]; // this is getting called when I press a button in the RN view
  callback(@[[NSNull null], [NSNumber numberWithInt:(number*number)]]);
}

@end
现在由于某种我不知道的原因,当调用
changeView
方法时,什么都没有发生。据我所知,
pushViewController
是使用
UINavigationController
更改视图时要调用的方法

现在,如果我想在
didfishlaunchwithoptions
方法中使用与
changeView
中相同的代码行来更改视图控制器,它可以完美地工作

[self.navigationController pushview控制器:[[SimpleViewController alloc]init]动画:是]

更奇怪的是,有一次我点击RN按钮约20次,然后突然视图开始改变约20次

另外,我想提到的是,当我按下RN按钮时,
changeView
总是被调用


在这件事上,如果有任何帮助,我将不胜感激。我在这个问题上纠缠了一段时间,我很确定我可能错过了一些明显的东西。谢谢

我终于解决了这个问题。更改ViewController时,必须从主线程执行此操作,否则它将无法按预期工作

我所需要做的就是将此方法添加到Starter的
实现中:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [super applicationDidFinishLaunching:application];  

  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"SomethingMobile"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  UIViewController *rootViewController = [[UIViewController alloc] init];
  rootViewController.view = rootView;


  self.navigationController = [[UINavigationController alloc] initWithRootViewController:   rootViewController];
  [self.window setRootViewController:self.navigationController];

//--- style the UINavigationController
  self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
  self.navigationController.navigationBar.topItem.title = @"Home";

  return YES;
}

-(void) changeView {
 // SimpleViewController is a valid UIViewController that I tested separately and it works
  [self.navigationController pushViewController:[[SimpleViewController alloc] init] animated:YES];
  self.navigationController.navigationBar.topItem.title = @"OF";
  printf("Got here");
}

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