RN 加载 iOS ViewController

大致就是这样一回事吧。
建议用 react-native init 创建项目后,再把旧的iOS项目将文件迁移到该iOS项目去。


OneViewController类

.h

#import <UIKit/UIKit.h>

@interface OneViewController : UIViewController

@end

.m

#import "OneViewController.h"

@interface OneViewController ()

@end

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 200, 50)];
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"点击按钮退出" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pressedButtonToDoSomething:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void) pressedButtonToDoSomething:(UIButton*)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end

AppDelegate 类:

.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property(nonatomic,strong) UIViewController* rootViewController;

@end

.m

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.app.bundle?platform=ios"];
    
    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"MyReactNativeApp"
                                                 initialProperties:nil
                                                     launchOptions:launchOptions];
    
    rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.rootViewController = [UIViewController new];
    
    self.rootViewController.view = rootView;
    
    self.window.rootViewController = self.rootViewController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end

MyVc 类:

.h

#import <Foundation/Foundation.h>

#import <React/RCTBridgeModule.h>

@interface MyVc : NSObject<RCTBridgeModule>

@end

.m

#import "MyVc.h"

#import <React/RCTBridge.h>

#import "AppDelegate.h"

#import "OneViewController.h"

@implementation MyVc

RCT_EXPORT_MODULE()

- (void) show {
    
    UIViewController *vc = [[OneViewController alloc]init];
    
    //要在主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [delegate.rootViewController presentViewController:vc animated:YES completion:nil];
    });
}

RCT_EXPORT_METHOD(showNew) {
    [self show];
}

@end

index.app.js

'use strict';
import React, { Component } from 'react';
import {
  AppRegistry,
  View,
  Image,
  Text,
  TouchableOpacity,
  NativeModules,
} from 'react-native';

class App extends Component {
  render() {
    return (
      <View>
          <TouchableOpacity style={{height:100,width:100,backgroundColor:'#ff6688'}}
                        onPress={
                            () => {NativeModules.MyVc.showNew()}
                        }>
            </TouchableOpacity>
      </View>
    );
  }

  componentDidMount() {
      
  }
}

AppRegistry.registerComponent('MyReactNativeApp', () => App);

还可以这么写:

'use strict';
import React, { Component } from 'react';
import {
  AppRegistry,
  View,
  Image,
  Text,
  TouchableOpacity,
  NativeModules,
} from 'react-native';

var MyVc = NativeModules.MyVc;

class App extends Component {
  render() {
    return (
      <View>
          <TouchableOpacity style={{height:100,width:100,backgroundColor:'#ff6688'}}
                        onPress={
                            () => {MyVc.showNew()}
                        }>
            </TouchableOpacity>
      </View>
    );
  }

  componentDidMount() {
      
  }
}

AppRegistry.registerComponent('MyReactNativeApp', () => App);