1
我正在創建自己的自定義ui組件,以便在objective-c中作出反應,並且迄今爲止一切正常,除非我似乎無法在組件上調用任何方法。這是我的目標cReact Native UI組件方法
#import "RNTWebViewManager.h"
#import <WebKit/WebKit.h>
@interface RNTWebViewManager()
@property (strong, nonatomic) WKWebView *webView;
@end
@implementation RNTWebViewManager
@synthesize webView;
RCT_EXPORT_MODULE()
- (UIView *)view {
webView = [[WKWebView alloc] init];
return webView;
}
RCT_EXPORT_METHOD(loadWebsite:(NSString *)url)
{
RCTLogInfo(@"Opening url %@",url);
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
};
@end
,這裏是我的相應的JavaScript代碼:
import React, { Component } from 'react';
import {
requireNativeComponent,
AppRegistry,
StyleSheet,
Button,
Text,
View
} from 'react-native';
// Load Native iOS Components
var RNTWebView = requireNativeComponent('RNTWebView', null);
class WebView extends Component {
render() {
return (
<RNTWebView style={styles.webView} />
)
}
}
export default class iOSComponents extends Component {
componentDidMount() {
RNTWebView.loadWebsite("http://www.google.com/");
}
render() {
return (
<View style={styles.webView}>
<View style={styles.statusBar} />
<WebView />
</View>
);
}
}
const styles = StyleSheet.create({
statusBar: {
height:22,
},
webView: {
flex:1,
}
});
AppRegistry.registerComponent('iOSComponents',() => iOSComponents);
當我嘗試調用RNTWebView.loadWebsite(「http://www.google.com」)我得到一個錯誤說RNTWebView.loadWebsite不一個函數。
非常感謝!它現在不會拋出錯誤,但它也不會更改webview。我像這樣導入模塊:讓RNTWebViewManager = NativeModules.RNTWebViewManager;然後調用該函數,但是它與我正在顯示的webview不同的實例嗎? – Asleepace
等一下,我知道了,謝謝! – Asleepace