React-Native:将React-Native View解除/退出回Native

时间:2016-02-09 17:17:28

标签: ios objective-c react-native

我有一个现有的应用程序,我正在努力为其中的一部分集成React-Native。我是 无法理解如何“退出”本地反应并返回原生视图。

以下是一些代码:

// Main objective-c code that bootstraps the react-native view. This view is loaded as a modal view.
MainViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];

    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"IndexApp" initialProperties:props launchOptions:nil];

    rootView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-49);
    [self.view addSubview:rootView];

}

我最初的反应意见如下:

render() {
  return (
      <Navigator
          style={styles.container}
...

我在导航器上有一个右导航按钮,我想“解除”反应视图和底层MainViewController原生视图。

我已经尝试从反应视图回调MainViewController,但是没有用:

RCT_EXPORT_METHOD(dismiss:(NSString *)name location:(NSString *)location)
{
    NSLog(@"dismiss...");
    // these don't do anything
    //[self dismissViewControllerAnimated:YES completion:nil];
    //[self.navigationController popViewControllerAnimated:YES];
    // anything with _rootView doesn't do anything, i.e. _rootView removeFromSuperview];

}

任何有关“退出”本机视图并返回本机视图的方法的帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

我发现这种方法的唯一方法是this

它的要点是:

  1. 在Obj-C中创建NotificationManager类并将其公开为React Module
  2. 在ViewController寄存器中获取通知,当收到触发器时[self dismissViewController ..]

答案 1 :(得分:1)

你需要在主线程上运行pop或dismiss:

RCT_EXPORT_METHOD(dismiss:(NSString *)name location:(NSString *)location)
{
    NSLog(@"dismiss...");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];

        // use pop instead if this view controller was pushed onto a navigation controller
        //[self.navigationController popViewControllerAnimated:YES];
    }
}

答案 2 :(得分:0)

注意:这没有“工作 - 我把它留在这里作为一个不起作用的例子。检查我的另一个答案是否有效。

如果以这种方式呈现MainViewController,popViewController:应该可以正常工作

NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
RCTRootView *reactView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                    moduleName:@"SimpleApp"
                                             initialProperties:nil
                                                 launchOptions:nil];


MainViewController *rootViewController = [MainViewController new];
rootViewController.view = reactView;

[[self navigationController] pushViewController:rootViewController animated:YES];