我如何从 React Native 获取值到我的桥 swift class?
How can i get values from react native into my bridge swift class?
根据文档,我尝试做这样的事情:
一切正常,但 myprop 的值没有进入我的 ComponentView
本地代码
//component.h
#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"
#import "React/RCTUIManager.h"
@interface component: RCTViewManager
@property (nonatomic, assign) NSString *myProp;
@end
//component.m
#import "component.h"
#import <Foundation/Foundation.h>
@interface RCT_EXTERN_MODULE(ComponentViewManager, RCTViewManager)
RCT_EXPORT_VIEW_PROPERTY(myProp, NSString)
@end
//ComponentViewManager.swift
import Foundation
@objc(ComponentViewManager)
class ComponentViewManager: RCTViewManager {
override func view() -> UIView! {
return ComponentView()
}
....
}
//ComponentView
class ComponentView: UIView {
//initWithFrame to init view from code
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
setupView()
}
func setupView() {
...
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func setMyProp(_ myProp: NSString) {
//DON'T GET Here :(
self.myProp = myProp
}
}
我做错了什么 myProp 值没有出现在我的视图中?
RN 文档:https://facebook.github.io/react-native/docs/native-components-ios
回答我自己的问题:(,但我想出了解决方法,如果可以帮助任何人。
您只需设置一个与注册到您的 .m 和 .h 文件中的 prop 同名的属性。
//ComponentView
@objc var myProp = "" {
didSet {
//Here you can call any function and it will be called after the value get here
doSomething()
}
}
根据文档,我尝试做这样的事情: 一切正常,但 myprop 的值没有进入我的 ComponentView
本地代码
//component.h
#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"
#import "React/RCTUIManager.h"
@interface component: RCTViewManager
@property (nonatomic, assign) NSString *myProp;
@end
//component.m
#import "component.h"
#import <Foundation/Foundation.h>
@interface RCT_EXTERN_MODULE(ComponentViewManager, RCTViewManager)
RCT_EXPORT_VIEW_PROPERTY(myProp, NSString)
@end
//ComponentViewManager.swift
import Foundation
@objc(ComponentViewManager)
class ComponentViewManager: RCTViewManager {
override func view() -> UIView! {
return ComponentView()
}
....
}
//ComponentView
class ComponentView: UIView {
//initWithFrame to init view from code
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
setupView()
}
func setupView() {
...
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func setMyProp(_ myProp: NSString) {
//DON'T GET Here :(
self.myProp = myProp
}
}
我做错了什么 myProp 值没有出现在我的视图中? RN 文档:https://facebook.github.io/react-native/docs/native-components-ios
回答我自己的问题:(,但我想出了解决方法,如果可以帮助任何人。
您只需设置一个与注册到您的 .m 和 .h 文件中的 prop 同名的属性。
//ComponentView
@objc var myProp = "" {
didSet {
//Here you can call any function and it will be called after the value get here
doSomething()
}
}