2016-03-16 11 views
5

Mam Natywny widok systemu iOS, który jest UIView z widokiem mapy i UIView. Mapa ma wydarzenie o nazwie "regionDidChangeAnimated", chcę wysłać zdarzenie do React Native. Ale responseTag nie ma racji.Reakcja Natywna iOS Natywna Wyświetl wyślij wydarzenie z właściwością reactTag

- (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]; 
} 

Odpowiedz

9

Jeśli spojrzeć w kodzie reagują-natywny dla rodzimych poglądów one realizowane:

https://github.com/facebook/react-native/tree/master/React/Views

wygląda dokumentacja jest nieaktualna i zamiast używać:

[self.bridge.eventDispatcher sendInputEventWithName... 

Powinieneś wykonać następujące czynności:

@property (nonatomic, copy) RCTBubblingEventBlock onTopChange; 

self.onTopChange(@{ 
    @"region": @{ 
    @"latitude": @(region.center.latitude), 
    @"longitude": @(region.center.longitude), 
    @"latitudeDelta": @(region.span.latitudeDelta), 
    @"longitudeDelta": @(region.span.longitudeDelta), 
    } 
}; 

Jest też RCTDirectEventBlock Nie jestem pewien, jaka jest różnica między tym a RCTBubblingEventBlock

Patrząc w RCTComponent.m liniach 160-169 powinien obsłużyć celu automatycznego ustawiania dla Ciebie:

// 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); 
}; 

także w klasie Manager dodaj:

RCT_EXPORT_VIEW_PROPERTY(onTopChange, RCTBubblingEventBlock) 

I nie zapomnij połączyć wydarzenia w swoim JSX:

<MyComponent onTopChange={this.handleOnTopChange}/> 
Powiązane problemy