Real user interface with Swift: how to access responseTag in event

When creating a custom user interface component in Swift, how can I access reactTagto trigger the React Native event?

Using the following code to create a custom component in Swift with a button: When the button is clicked, using bridge.eventDispatcherto sendInputEventWithNameshould trigger an event in the React Native JavaScript code. This is done by passing the responseTag as part of the event dictionary. However, it is not clear to me how to access the responseTag property or even where it is available.

(Digging around a bit, it seems to be present on something called RCTShadowView, however, responseTag is always nilon my shadowView)

Swift

//  FooView.swift
import UIKit


@objc(FooView)
class FooView: UIView {
    @objc var bridge: RCTBridge!

    required override init(frame: CGRect) {
        super.init(frame: frame)
        let button = UIButton(frame: CGRect(x: 10, y: 10, width: 244, height: 244))
        button.addTarget(self, action: "buttonClicked:", forControlEvents: .TouchUpInside)
        self.addSubview(button)
    }

    func buttonClicked(sender: AnyObject?) {
        let event = [
            "target": "", // <-- how can one get access to the reactTag property?
        ]

        self.bridge.eventDispatcher.sendInputEventWithName("topChange", body: event)
    }
}

Goal c

//  FooViewBridge.m

#import "RCTBridgeModule.h"
#import "RCTViewManager.h"
#import "FooProj-Swift.h"


@interface RCTFooViewManager : RCTViewManager
@end


@implementation RCTFooViewManager


RCT_EXPORT_MODULE()

- (UIView *)view
{
    FooView* fooView = [[FooView alloc] init];
    fooView.bridge = self.bridge;
    fooView.shadowView = self.shadowView; // <-- was hoping this would have a reactTag but it always nil
    return fooView;
}


@end
+4
1

FooView.swift self.reactTag, UIView+React.h.

Bridging-Header.h.

#import "UIView+React.h"

: , , sendInputEventWithName, Swifty

+1

Source: https://habr.com/ru/post/1614325/


All Articles