React-Native iOS - 如何通过按下按钮从React-Native视图导航到非React-Native视图(本机iOS视图控制器)? [英] React-Native iOS - How can I navigate to a non-React-Native view (native iOS view controller) from a React-Native view with a button press?

查看:184
本文介绍了React-Native iOS - 如何通过按下按钮从React-Native视图导航到非React-Native视图(本机iOS视图控制器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RN doco和其他示例显示了如何从本机iOS视图控制器启动React-Native视图,但不是相反。有人可以解释我怎么做吗?

The RN doco and other examples show how to launch a React-Native view from a native iOS view controller, but not the other way around. Can someone explain how I can do this?

推荐答案

我能够解决这个问题。在我的例子中,我使用我自己的Swift本机视图控制器的Obj-C基础项目(这是RN默认值)。我的解决方案就在这里以防其他人出现:

I was able to figure this out. In my case, I am using an Obj-C base project (which is the RN default) with my own Swift native view controller. My solution is here in case this comes up for anyone else:

简单地说,答案是使用RTCBridge模块允许RN javascript调用本机iOS方法。

Simply put, the answer is to use an RTCBridge module to allow the RN javascript to call a native iOS method.

这是组件的概述,然后是实现:

Here is an outline of the components, followed by the implementation:


  1. AppDelegate.h / .m - 为初始RN视图初始化RN javascript索引文件,还设置了将根视图控制器交换到本机视图控制器的方法(此方法将从RTCBridge模块调用。

  2. MyViewController.swift - 具有标准实现的普通UIViewController。

  3. MyProject-Bridging-Header.h - 提供Obj-C< - > Swift通信

  4. ChangeViewBridge.h / .m - 这提供了绑定,允许您从RN javascript调用本机iOS方法

  5. index.ios.js - 初始化自定义RCTBridge模块并调用绑定方法,按下按钮切换到本机视图。

  1. AppDelegate.h/.m - Initialize the RN javascript index file for the initial RN view, also setup a method to swap the root view controller to a native view controller (this method will be called from the RTCBridge module.
  2. MyViewController.swift - A normal UIViewController with a standard implementation.
  3. MyProject-Bridging-Header.h - provides Obj-C <-> Swift communication
  4. ChangeViewBridge.h/.m - This provides the binding to allow you to call native iOS methods from the RN javascript
  5. index.ios.js - Initialize your custom RCTBridge module and call the bound method to switch to your native view with a button press.






AppDelegate.h


AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, IXUAFDelegate> {
  NSDictionary *options;
  UIViewController *viewController;
}

@property (nonatomic, strong) UIWindow *window;

- (void) setInitialViewController;
- (void) goToRegisterView; // called from the RCTBridge module

@end






AppDelegate.m


AppDelegate.m

#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "FidoTestProject-Swift.h" // Xcode generated import to reference MyViewController.swift from Obj-C

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  options = launchOptions;
  [self setInitialViewController];
  return YES;
}

- (void) setInitialViewController {
  NSURL *jsCodeLocation;

  jsCodeLocation = [NSURL URLWithString:@"http://192.168.208.152:8081/index.ios.bundle?platform=ios&dev=true"];

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

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;

  viewController = rootViewController;

  [self.window makeKeyAndVisible];
}

// this method will be called from the RCTBridge
- (void) goToNativeView {
  NSLog(@"RN binding - Native View - MyViewController.swift - Load From "main" storyboard);
  UIViewController *vc = [UIStoryboard storyboardWithName:@"main" bundle:nil].instantiateInitialViewController;
  self.window.rootViewController = vc;
}

@end






MyViewController .swift


MyViewController.swift

class RegisterViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print("MyViewController loaded...")
        // standard view controller will load from RN
    }
}






MyProject-Bridging-Header.h


MyProject-Bridging-Header.h

@import Foundation;
@import UIKit;
@import CoreLocation;
@import AVFoundation;

#import "React/RCTBridge.h"
#import "React/RCTBridgeModule.h"
#import "React/RCTBundleURLProvider.h"
#import "React/RCTRootView.h"
#import "AppDelegate.h"






ChangeViewBridge.h


ChangeViewBridge.h

#import <React/RCTBridgeModule.h>

@interface ChangeViewBridge : NSObject <RCTBridgeModule>

- (void) changeToNativeView;

@end






ChangeViewBridge.m


ChangeViewBridge.m

#import "RegisterBridge.h"
#import "FidoTestProject-Swift.h"
#import "AppDelegate.h"

@implementation ChangeViewBridge

// reference "ChangeViewBridge" module in index.ios.js
RCT_EXPORT_MODULE(ChangeViewBridge);

RCT_EXPORT_METHOD(changeToNativeView) {
  NSLog(@"RN binding - Native View - Loading MyViewController.swift");
  AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  [appDelegate goToNativeView];
}

@end






index.ios.js


index.ios.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

'use strict';

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Alert,
  Text,
  View,
  NativeModules,
  TouchableHighlight
} from 'react-native';

export default class FidoTestProject extends Component {

  constructor(props) {
     super(props)
     this.done = false;
   }

    _changeView() {
      this.done = true;
      this.render();
      NativeModules.ChangeViewBridge.changeToNativeView();
    }

  render() {
    if (!this.done) {
      return (
        <View style={styles.container}>
          <TouchableHighlight onPress={() => this._changeView()}>
            <Text color="#336699">
              Press to Change to Native View
            </Text>
          </TouchableHighlight>
        </View>
      );
    } else {
      return (<View></View>);
    }
  }
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  }
});

AppRegistry.registerComponent('FidoTestProject', () => FidoTestProject);

这篇关于React-Native iOS - 如何通过按下按钮从React-Native视图导航到非React-Native视图(本机iOS视图控制器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆