ios – Share UIView occasion between class

0
21
ios – Share UIView occasion between class


I am making an attempt to share my UIView between some class for decode body. For this, i’ve add sharedInstance technique for get the occasion from different class.

This technique identical not work and return an new occasion, not the present.

My UIView class :

utilizing namespace fb::react;

@interface RTNViewStreamView () 

@finish

@implementation RTNViewStreamView {
    UIView * _view;
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
    return concreteComponentDescriptorProvider();
}

- (instancetype)initWithFrame:(CGRect)body
{
  if (self = [super initWithFrame:frame]) {
    static const auto defaultProps = std::make_shared();
    _props = defaultProps;

    _view = [[UIView alloc] init];
      NSLog(@"initWithFrame: %p", _view);

    self.contentView = _view;
  }

  return self;
}

+ (id)sharedInstance
{
    static RTNViewStreamView *sharedClassA = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClassA = [[self alloc] init];
    });
    return sharedClassA;
}

- (UIView *)getContentView {
    return self.contentView;
}

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
    const auto &oldViewProps = *std::static_pointer_cast(_props);
    const auto &newViewProps = *std::static_pointer_cast(props);

    if (oldViewProps.shade != newViewProps.shade) {
        NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.shade.c_str()];
        [_view setBackgroundColor:[self hexStringToColor:colorToConvert]];
    }

    [super updateProps:props oldProps:oldProps];
}

Class RTNViewStreamViewCls(void)
{
    return RTNViewStreamView.class;
}

Then, from different class, i do that :

- (instancetype)init {
    self = [super init];
    if (self) {
        RTNViewStreamView *classA = [RTNViewStreamView sharedInstance];
        NSLog(@"init rtnstream: %p", classA.contentView);
        classA.contentView.backgroundColor = [UIColor redColor]; // Not work
    }
    return self;
}

From logs, i see the initWithFrame technique is re-run when i name the sharedInstance from different class :

enter image description here

I do not actually perceive why i am unable to retrieve my view occasion? i want to make use of this occasion to switch it some other place.

I am on React-Native challenge, i write TurboModule with Cloth.

LEAVE A REPLY

Please enter your comment!
Please enter your name here