当视图第一次显示时,js事件运行正常。比我使用presentViewController
来显示我点击的图像。在dismissViewController
之后,返回到webView,再次单击该图像,但js callHandler未运行。这是我的代码。
// oc file code
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (_bridge) { return; }
_bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView];
[_bridge registerHandler:@"jsImagesList" handler:^(id data, WVJBResponseCallback responseCallback) {
self.imageList = data;
}];
[_bridge registerHandler:@"openImageOnIndex" handler:^(id data, WVJBResponseCallback responseCallback) {
[self openImageOnIndex:data];// use MWPhotoBrowser to show the image
}];
}
// js file code
function setupWebViewJavascriptBridge(callback) {
// just copy from WebViewJavascriptBridge
}
setupWebViewJavascriptBridge(function(bridge) {
var objs = document.getElementsByTagName("img");
var images = [];
for(var i=0;i<objs.length;i++) {
images.push(objs[i].src);
objs[i].setAttribute('data-index',i);
objs[i].onclick = function(){
// just for test, let me know I can click the img
document.body.style.backgroundColor = '#'+Math.floor(Math.random()*0xFFFFFF).toString(16);
var di = this.getAttribute('data-index');
// ----------- question -----------
// first time it run, but when dismissViewController, it not run
bridge.callHandler('openImageOnIndex', {'i': di}, function(response) {})
}
}
bridge.callHandler('jsImagesList', images, function(response) {})
})
我该如何修改它,非常感谢!