鍍金池/ 問答/iOS  HTML/ [ios] fireGlobalEvent無響應(yīng)!寫法是否有問。

[ios] fireGlobalEvent無響應(yīng)!寫法是否有問。

做一個(gè)二維碼掃描功能

在 appdelegate.m里初始化了 instance單例

#import <UIKit/UIKit.h>
#import <WeexSDK/WXSDKInstance.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) WXSDKInstance *instance;
@end

注冊(cè)了module 初始化instance

 self.instance = [[WXSDKInstance alloc] init];
 [WXSDKEngine registerModule:@"scanEvent" withClass:[WXScanModule class]];

然后 module跳轉(zhuǎn)掃描頁面

-(void)startScan{

    UIViewController *vc = [currentView getCurrentVC];
    ScanViewController *scan = [[ScanViewController alloc] init];
    
  [vc presentViewController:[[UINavigationController alloc] initWithRootViewController:scan] animated:YES completion:nil];
    
}

最后在 ScanViewController 發(fā)送 fireGlobalEvent 掃描出了東西 weex頁面接受不到這個(gè)fireGlobalEvent …

-(void)getScanDataString:(NSString*)scanDataString{
    
    NSLog(@"二維碼內(nèi)容:%@",scanDataString);
    AppDelegate *appDele = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [appDele.instance fireGlobalEvent:@"globalScan" params:@{@"scanInfo":scanDataString}];
 
    [self goBack];

}

大神們看看這個(gè)代碼有啥問題啊~~

回答
編輯回答
撿肥皂

這個(gè)事件只會(huì)發(fā)到instance 對(duì)應(yīng)的那個(gè)頁面,如果需要發(fā)送到所有頁面,可以直接使用

[[NSNotificationCenter defaultCenter] postNotificationName:@"globalEventName" object:nil userInfo:[NSDictionary dictionaryWithObject:@"key and value" forKey:@"params"]];

全局事件會(huì)把userInfo[@"param"] 傳遞給js事件

2018年1月9日 22:11