OGeek|极客世界-中国程序员成长平台

标题: javascript - react 原生 : Call method of RCTViewManager and Render a View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:02
标题: javascript - react 原生 : Call method of RCTViewManager and Render a View

在 React Native 中,是否可以渲染 RCTBridgeModule 的 UIView 并调用此 Module 的方法? 下面我发布了我用两种方法创建的模块。但我不知道它是否正确:

RCTAugmentPlayerManager.h

#import "RCTBridgeModule.h"

@interface RCTAugmentPlayerManager : NSObject <RCTBridgeModule>
@end

RCTAugmentPlayerManager.m

@implementation RCTAugmentPlayerManager

RCT_EXPORT_MODULE();

// Method which execute treatment 
RCT_EXPORT_METHOD(oneMethodNSString *)name )
{
  RCTLogInfo(@"Display name %@", name);
}

// Method which return a view to render in Javascript
RCT_EXPORT_METHOD(methodWithReturnView)
{
   UIView * view = [[UIView alloc] init];
   return view;
}
@end

AugmentPlayer.js

import { NativeModules } from 'react-native';
var RCTAugmentPlayerManager = NativeModules.RCTAugmentPlayerManager;

index.ios.js

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

class mobileApp extends Component {

    constructor(props){
        super(props);

        var augment = NativeModules.AugmentPlayer; 
        augment.oneMethod('test');
    }
  render() {

    return (

    <View style={{height:200, width:200}}>      
        {AugmentPlayer.methodWithReturnView}
    </View>
    );
  }
}    

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

谢谢



Best Answer-推荐答案


TL;DR:不,你不能。

要渲染 UIView,您必须继承 RCTViewManager 并在 view 方法中返回您的 View :

@interface MyCoolViewManager: RCTViewManager
@end

@implementation MyCoolViewManager

RCT_EXPORT_MODULE()

-(UIView *)view {
   return [MyCoolView new];
}

@end

有关更多信息,请参阅文档,其中非常详细地说明了您需要做什么:

http://facebook.github.io/react-native/docs/native-components-ios.html

关于javascript - react 原生 : Call method of RCTViewManager and Render a View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38500614/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4