【问题标题】:React-Native: Dismiss/Exit React-Native View back to NativeReact-Native: Dismiss/Exit React-Native View 回到 Native
【发布时间】:2016-05-19 19:09:02
【问题描述】:

我有一个现有的应用程序,我正在为它的一部分集成 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}
...

我在 Navigator 上有一个右导航按钮,我想“关闭”反应视图和底层 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];

}

任何有关“退出”反应原生视图并返回原生视图的方法的帮助将不胜感激。

【问题讨论】:

  • “我已经尝试回调 MainViewController”,你是怎么做到的?

标签: ios objective-c react-native


【解决方案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】:

注意:这不起作用 - 我在这里留下了这个作为不起作用的示例。查看我的其他答案以了解可行的方法。

如果你以这种方式显示 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];

【讨论】:

    【解决方案3】:

    我发现这个工作的唯一方法是this

    它的要点是:

    1. 在 Obj-C 中创建 NotificationManager 类并将其公开为 React 模块
    2. 在 ViewController 中注册一个通知,当接收到触发器 [self dismissViewController..]

    【讨论】:

    • 完美运行!我想知道已经 4 年了,是否有一些更新的更简单/更酷的方法来做同样的事情?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2016-05-27
    相关资源
    最近更新 更多