iOS 部分页面使用React Native

如果项目中单纯只是用RN来开发的话,可能就用不上我这篇文章,但是实际开发中,RN和OC原生混合开发的还是占主要位置,下面就来讲讲如何部分页面用RN来写,这次讲得示例是整个项目创建的方式是RN的命令创建的,如果是在原有项目中集成RN到里面去,那么前面还有一些相关的步骤,后面有时间我再整理出一篇相关的文章进行讲解,这么就默认当前通过RN命令的方式创建好了一个项目,下面就来看看相关的代码;

AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
/**<#属性#>*/
@property (nonatomic,strong)NSURL *jsCodeLocation;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  .
  .
  .
 #ifdef DEBUG
  self.jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
 #else
  self.jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
 #endif
  return YES;
}

1、加载A页面,创建一个控制器A

ReactNativeAController.h文件

#import <UIKit/UIKit.h>
#import <React/RCTRootView.h>
@interface ReactNativeAController : UIViewController
/**rootView*/
@property (nonatomic,strong)RCTRootView *rootView;
@end
ReactNativeAController.m文件
#import "ReactNativeAController.h"
@interface ReactNativeAController ()

@end
@implementation TTReactNativeBasseController
- (void)viewDidLoad{
  [super viewDidLoad];
  NSDictionary *parame =@{};
   self.rootView= [[RCTRootView alloc] initWithBundleURL:SharedAppDelegate.jsCodeLocation moduleName:@"YunTianZhiHui" initialProperties:parame  launchOptions:nil];
  self.rootView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  [self.view addSubview:self.rootView];
}
@end

2、加载B页面

加载B页面跟A页面的加载方式是一样的,唯一需要改的就是
moduleName:@"YunTianZhiHui" 改成index.ios 文件中对应的
moduleName: @" Recommand"
1>>>>index.ios 文件中定义
import React, { Component } from 'react';
import {
    AppRegistry
} from 'react-native';
import YunTianZhiHui from './tiantian/index'
import Recommand from './tiantian/index/index_classfication'


AppRegistry.registerComponent('YunTianZhiHui', () => YunTianZhiHui);
AppRegistry.registerComponent('Recommand', () => Recommand);
'./tiantian/index'  文件定义
import React, { Component } from 'react';
import {
    Navigator,
    AsyncStorage
} from 'react-native';
import EbookReading from '../container/read/two/ebook_reading';
//ebook_reading.js文件就是当前加载的页面

export default class YunTianZhiHui extends Component {
render() {
        let defaultName = 'EbookReading';
//ebook_reading.js文件中 export default class EbookReading extends Component ,注意类名要写成EbookReading 更这里的名称要统一
        let defaultComponent = EbookReading;
        return (
            <Navigator
                initialRoute={{ name: defaultName, component: defaultComponent,params:{orgGid:this.props.orgGid,passportGid:this.props.passportGid,ebookNum:this.props.ebookNum}}}
                configureScene={(route) => {
                return Navigator.SceneConfigs.FloatFromRight;
              }}
                renderScene={(route, navigator) => {
                let Component = route.component;
                return <Component {...route.params} navigator={navigator} />
              }} />
        );
    }

    handleResult(error,result){
        if (error != null) {
            console.log('error message:' + error.message);
            return;
        }
        if (result == null) {
            //没有指定的key
            return;
        }
        return result;
    }
}
index_classfication文件的定义跟'./tiantian/index'  文件定义是一样的,
区别就是:
import TwoRecommand from '../container/recommend/two/two_recommand';
export default class Recommand extends Component {

render() {
        let defaultName = 'TwoRecommand';
        let defaultComponent = TwoRecommand;

}
}

以上是自己在项目开发过程中摸索出来的,采取的是路由开发模式,通过一个管理中心,分散各个路由接口,相信需要加载单个RN页面的能用得上,如果有不同见解,或者有需要纠正的地方,欢迎留言,相互学习!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,285评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 钱穆高中没读完,以没大学经历为憾,然北大教授八年,讲中国通史,操无锡官话,每堂两小时,三百座教室站无虚位,甚者连听...
    晚霞消失之时阅读 353评论 0 1
  • 本文主要摘自Hibernate缓存,整理些要点出来 Hibernate查询缓存依赖于二级缓存。当启用查询缓存后,第...
    liveaswind阅读 2,692评论 0 0
  • -----太快了 假期余额不足了 能充值吗 -----不能 -----哦,好吧 -----嗯
    空空的小et阅读 128评论 0 0