Closed hechengloong closed 7 years ago
既然是callback,为什么还用fireGlobalEvent?
var globalEvent weex_define('@weex-temp/api', function (weex_require) { globalEvent = __weex_require__('@weex-module/globalEvent') });
exports.addEventListener = function (eventName, callback) { globalEvent.addEventListener(eventName, callback); }
Utils.addEventListener("eventName", function (param) { //do something });
[_instance fireGlobalEvent:@"eventName" params:@{@"key":@"value"}];
@liwenyou 自己写了一个 baseViewController 想利用fireGlobalEvent进行回调 该如何更改呢
//
// WX_BaseViewController.m
// Workbench
//
// Created by MT on 16/11/24.
// Copyright © 2016年 MT. All rights reserved.
//
#import "WX_BaseViewController.h"
#import "WXRootViewController.h"
#import "WXSDKInstance.h"
#import "WXSDKInstance_private.h"
#import "WXSDKEngine.h"
#import "WXSDKManager.h"
#import "WXUtility.h"
#import "GuidePageViewController.h"
@interface WX_BaseViewController ()
@property (nonatomic, strong) WXSDKInstance *instance;
@property (nonatomic, strong) UIView *weexView;
@property (nonatomic, strong) NSURL *sourceURL;
@property (nonatomic,strong)NSMutableDictionary * WXData;
@property (nonatomic,assign)BOOL needResult;
@property (nonatomic, strong) NSString * filename;
@end
@implementation WX_BaseViewController
-(NSMutableDictionary *)WXData{
if (!_WXData) {
_WXData = [NSMutableDictionary dictionary];
}
return _WXData;
}
- (void)dealloc
{
[_instance destroyInstance];
[self _removeObservers];
}
- (instancetype)initWithSourceURL:(NSURL *)sourceURL Dic:(NSDictionary*)dic;
{
if ((self = [super init])) {
self.sourceURL = sourceURL;
self.hidesBottomBarWhenPushed = YES;
NSDictionary * WXData = [dic objectForKey:@"data"];
if (WXData) {
[self.WXData setValue:WXData forKey:@"wxPushInit"];
}else{
self.WXData = nil;
}
if ([dic objectForKey:@"needResult"])
{
//需要返回值
self.needResult = YES;
}
if ([[dic objectForKey:@"needFinish"] isEqualToString:@"0"])
{
//不需要返回键;
[self.navigationController removeFromParentViewController];
}
[self _addObservers];
}
return self;
}
- (instancetype)initWithWeFileName:(NSString *)filename
{
if (self = [super init]) {
NSString *urlString = [NSString stringWithFormat:@"%@%@",kWeex_URL,filename];
self.sourceURL = [NSURL URLWithString:urlString];
self.hidesBottomBarWhenPushed = YES;
self.WXData = nil;
[self _addObservers];
self.filename = filename;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 隐藏导航栏
[self.navigationController setNavigationBarHidden:YES];
if ([self.filename isEqualToString:@"home.js"]) {
[self._network WBRefreshToken:[PDataManager GetLoginToken]];
}
}
/**
* After setting the navbar hidden status , this function will be called automatically. In this function, we
* set the height of mainView equal to screen height, because there is something wrong with the layout of
* page content.
*/
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
if ([self.navigationController isKindOfClass:[WXRootViewController class]]) {
CGRect frame = self.view.frame;
frame.origin.y = 0;
frame.size.height = [UIScreen mainScreen].bounds.size.height;
self.view.frame = frame;
}
}
/**
* We assume that the initial state of viewController's navigitonBar is hidden. By setting the attribute of
* 'dataRole' equal to 'navbar', the navigationBar hidden will be NO.
*/
- (void)viewDidLoad
{
[super viewDidLoad];
[self addEdgePop];
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
// // handleNavigationTransition:为系统私有API,即系统自带侧滑手势的回调方法,我们在自己的手势上直接用它的回调方法
// UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// panGesture.delegate = self; // 设置手势代理,拦截手势触发
// [self.view addGestureRecognizer:panGesture];
// 一定要禁止系统自带的滑动手势
// self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
[self _renderWithURL:_sourceURL];
if ([self.navigationController isKindOfClass:[WXRootViewController class]]) {
self.navigationController.navigationBarHidden = YES;
}
}
- (void)addEdgePop
{
UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
edgePanGestureRecognizer.delegate = self;
edgePanGestureRecognizer.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:edgePanGestureRecognizer];
}
- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer*)edgePanGestureRecognizer{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark- UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (!self.navigationController || [self.navigationController.viewControllers count] == 1) {
return NO;
}
return YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self _updateInstanceState:WeexInstanceAppear];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self _updateInstanceState:WeexInstanceDisappear];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[self _updateInstanceState:WeexInstanceMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)refreshWeex
{
[self _renderWithURL:_sourceURL];
}
- (void)_renderWithURL:(NSURL *)sourceURL
{
if (!sourceURL) {
return;
}
[_instance destroyInstance];
_instance = [[WXSDKInstance alloc] init];
_instance.frame = CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, self.view.bounds.size.height);
_instance.pageObject = self;
_instance.pageName = [[WXUtility urlByDeletingParameters:sourceURL] absoluteString];
_instance.viewController = self;
NSString *newURL = nil;
if ([sourceURL.absoluteString rangeOfString:@"?"].location != NSNotFound) {
newURL = [NSString stringWithFormat:@"%@&random=%d", sourceURL.absoluteString, arc4random()];
}
else
{
newURL = [NSString stringWithFormat:@"%@?random=%d", sourceURL.absoluteString, arc4random()];
}
[_instance renderWithURL:[NSURL URLWithString:newURL] options:@{@"bundleUrl":sourceURL, @"locale": [PDataManager GetLocale]} data:self.WXData];
__weak typeof(self) weakSelf = self;
_instance.onCreate = ^(UIView *view) {
[weakSelf.weexView removeFromSuperview];
weakSelf.weexView = view;
[weakSelf.view addSubview:weakSelf.weexView];
};
_instance.onFailed = ^(NSError *error) {
};
_instance.renderFinish = ^(UIView *view) {
[weakSelf _updateInstanceState:WeexInstanceAppear];
};
}
- (void)_updateInstanceState:(WXState)state
{
if (_instance && _instance.state != state) {
_instance.state = state;
if (state == WeexInstanceAppear) {
[[WXSDKManager bridgeMgr] fireEvent:_instance.instanceId ref:WX_SDK_ROOT_REF type:@"viewappear" params:nil domChanges:nil];
}
else if (state == WeexInstanceDisappear) {
[[WXSDKManager bridgeMgr] fireEvent:_instance.instanceId ref:WX_SDK_ROOT_REF type:@"viewdisappear" params:nil domChanges:nil];
}
}
}
- (void)_appStateDidChange:(NSNotification *)nofity
{
if ([nofity.name isEqualToString:@"UIApplicationDidBecomeActiveNotification"]) {
[self _updateInstanceState:WeexInstanceForeground];
}
else if([nofity.name isEqualToString:@"UIApplicationDidEnterBackgroundNotification"]) {
[self _updateInstanceState:WeexInstanceBackground]; ;
}
}
- (void)_addObservers
{
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
UIApplicationDidEnterBackgroundNotification]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_appStateDidChange:)
name:name
object:nil];
}
}
- (void)_removeObservers
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)networkStoped:(CTNetwork *)network success:(int)success
{
[self removeWaitingView];
if (success==Result_Succeed)
{
if (network._netTag==WBNetworkTag_RefreshToken)// 刷新服务令牌
{
if ([network._data[@"return_code"] intValue] == 0)
{
NSLog(@"data = %@",network._data[@"data"]);
[PDataManager SetToken:[NSString stringWithFormat:@"%@",network._data[@"data"][@"authInfo"][@"token"]]];// 给token赋值
[PDataManager SetLoginToken:[NSString stringWithFormat:@"%@",network._data[@"data"][@"authInfo"][@"loginToken"]]];// 给loginToken赋值
} else {
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
// message:network._data[@"return_message"]
// delegate:self
// cancelButtonTitle:nil
// otherButtonTitles:@"确定", nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"登录状态已过期,请重新登录。"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"确定", nil];
[alert show];
}
}
}
else
{
[self showCustomAlert:network._data[@"return_message"]];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonInde
{
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
GuidePageViewController *guideVC = [[GuidePageViewController alloc] initWithTitle:@"" HTMLStringWithResource:@"guide" ofType:@"html" AndDomainName:kDomainName];
[vcs removeAllObjects];
[vcs addObject:guideVC];
self.navigationController.viewControllers = vcs;
[self.navigationController popToViewController:guideVC animated:YES];
}
@end
请问 iOS callBack 回调 想用fireGlobalEvent方法 在哪设置