有一些Objc代码示例如何将事件发送到@R_404_3715@,但我需要在swift中做什么?
#import "RCTBrIDge.h"#import "RCTEventdispatcher.h"@implementation CalendarManager@synthesize brIDge = _brIDge;- (voID)calendarEventReminderReceived:(NSNotification *)notification{ Nsstring *eventname = notification.userInfo[@"name"]; [self.brIDge.eventdispatcher sendAppEventWithname:@"EventReminder" body:@{@"name": eventname}];}@end我只是想弄清楚自己.这实际上非常简单.继承人我是怎么做到的:
EventTests.m
#import "RCTBrIDgeModule.h"@interface RCT_EXTERN_MODulE(EventTests,NSObject)RCT_EXTERN_METHOD( testEvent:(Nsstring *)eventname )@end
EventTests.Swift
import UIKit@objc( EventTests )class EventTests: NSObject { // Swift doesn't have synthesize - just define the variable var brIDge: RCTBrIDge! @objc func testEvent( eventname: String ) { self.brIDge.eventdispatcher.sendAppEventWithname( eventname,body: "Woot!" ) }}
MyModule.Js
var React = require( 'react-native' );var EventTests = require( 'NativeModules' ).EventTests;var { Component,NativeAppEventEmitter} = React;var testEventname = 'test';class MyModule extends Component { constructor( options ) { super( options ); // Register for our test event NativeAppEventEmitter.addListener( testEventname,( body ) => { console.log( body ); }); // Call objective c function,which will emit our test event EventTests.testEvent( testEventname ); }}module.exports = MyModule;
还要确保在桥接标头中包含一些导入:
#import "RCTBrIDge.h"#import "RCTEventdispatcher.h"总结
以上是内存溢出为你收集整理的在Swift中将本机发送事件发送到JavaScript全部内容,希望文章能够帮你解决在Swift中将本机发送事件发送到JavaScript所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)