使用正确的reactTag响应本机iOS本机视图发送事件

使用正确的reactTag响应本机iOS本机视图发送事件,ios,objective-c,uiview,react-native,Ios,Objective C,Uiview,React Native,我有一个iOS本机视图,它是一个带有地图视图和UIView的UIView。该映射有一个名为“regionDidChangeAnimated”的事件,我想将该事件发送到React本机。但这个标签是不对的 - (UIView *)view { CGRect screenRect = [[UIScreen mainScreen] bounds]; UIView *frameView = [[UIView alloc] initWithFrame:screenRect]; CGRect

我有一个iOS本机视图,它是一个带有地图视图和UIView的UIView。该映射有一个名为“regionDidChangeAnimated”的事件,我想将该事件发送到React本机。但这个标签是不对的

- (UIView *)view
{
  CGRect screenRect = [[UIScreen mainScreen] bounds];

  UIView *frameView = [[UIView alloc] initWithFrame:screenRect];

  CGRect frameRect = frameView.bounds;

  MAMapView *mapView;
  mapView = [[MAMapView alloc] initWithFrame:frameRect];
  self.mapview = mapView;
  mapView.delegate = self;

  [frameView addSubview:mapView];

  RCTFixedPin* pin = [[RCTFixedPin alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 260)];
  pin.userInteractionEnabled = NO;
  [frameView addSubview:pin];

  return frameView;
}

- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

  if (self.dragging) {
    self.dragging = NO;
  }


  MACoordinateRegion region = mapView.region;
  NSDictionary *event = @{
                          ***@"target": ,***
                          @"region": @{
                              @"latitude": @(region.center.latitude),
                              @"longitude": @(region.center.longitude),
                              @"latitudeDelta": @(region.span.latitudeDelta),
                              @"longitudeDelta": @(region.span.longitudeDelta),
                              }
                          };
  [self.bridge.eventDispatcher sendInputEventWithName:@"topChange" body:event];
}

如果在react本机代码中查找它们已实现的本机视图:

文档似乎已过期,而不是使用:

[self.bridge.eventDispatcher sendInputEventWithName...
您应该执行以下操作:

@property (nonatomic, copy) RCTBubblingEventBlock onTopChange;

self.onTopChange(@{
  @"region": @{
    @"latitude": @(region.center.latitude),
    @"longitude": @(region.center.longitude),
    @"latitudeDelta": @(region.span.latitudeDelta),
    @"longitudeDelta": @(region.span.longitudeDelta),
  }
};
还有一个
RCTDirectEventBlock
我不确定它与
RCTBubblingEventBlock

查看
RCTComponent.m
第160-169行,它应该自动为您处理目标设置:

// Special case for event handlers
__weak RCTViewManager *weakManager = _manager;
setterBlock = ^(id target, __unused id source, id json) {
  __weak id<RCTComponent> weakTarget = target;
  ((void (*)(id, SEL, id))objc_msgSend)(target, setter, [RCTConvert BOOL:json] ? ^(NSDictionary *body) {
    body = [NSMutableDictionary dictionaryWithDictionary:body];
    ((NSMutableDictionary *)body)[@"target"] = weakTarget.reactTag;
    [weakManager.bridge.eventDispatcher sendInputEventWithName:RCTNormalizeInputEventName(name) body:body];
  } : nil);
};
不要忘记在JSX中实际连接事件:

<MyComponent onTopChange={this.handleOnTopChange}/>

<MyComponent onTopChange={this.handleOnTopChange}/>