从本地code调用Javascript函数(IOS) [英] Calling Javascript function from native code (iOS)

查看:179
本文介绍了从本地code调用Javascript函数(IOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的故事板。我已经使用SWRevealViewController插件,在科尔多瓦的应用我的身边菜单。补充说:主视图控制器作为类参考上次视图控制器(具有导航栏项)。

在刷卡从左至右我科尔多瓦观点,就会发现Sidemenu。在点击我拯救选择行值

   - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {
    //菜单项是一个数组使用的我填充sidemenu
    * NSString的海峡= [菜单项objectAtIndex:indexPath.row];
}

我只想这个值传递给JavaScript函数。

我的定义javascript函数中的index.html

 <脚本>
    (函数(){
     window.execute =功能(){
     警报(町);
     VAR EL =的document.getElementById('搜索');
     VAR范围= angular.element(EL).scope();
     范围。$应用(函数(){
                  scope.execute();
                  });     }
     })();
    < / SCRIPT>    调用使用此功能
      * NSString的jsCommand = [的NSString stringWithFormat:@执行()];
    [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];


解决方案

尝试改变你的本地code为:

   - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {
    //菜单项是一个数组使用的我填充sidemenu
    * NSString的海峡= [菜单项objectAtIndex:indexPath.row];
    * NSString的jsCommand = [的NSString stringWithFormat:@执行(%@'),STR]
    [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
}

和你的JavaScript函数声明为:

  window.execute =功能(STR){...}

您会发现你的价值传递到 STR 函数参数。

This is my Story Board. I have used SWRevealViewController plugin for my side menu in cordova application. Added "Main View Controller" as class reference to the Last view controller(having item in nav-bar) .

On swiping from left to right on my cordova view will reveal Sidemenu. On click I'm saving selected row value in

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // menuItems is an array using which I'm populating the sidemenu
    NSString *  str = [menuItems objectAtIndex:indexPath.row];
}

I just want to pass this value to the javascript function.

Defined my javascript function in index.html

    <script >
    (function () {
     window.execute = function () {
     alert(" machi");
     var el = document.getElementById('search');
     var scope = angular.element(el).scope();
     scope.$apply(function() {
                  scope.execute();
                  });

     }
     })();
    </script>

    calling this function using
      NSString *jsCommand = [NSString stringWithFormat:@"execute()"];
    [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];

解决方案

Try to change your native code to:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // menuItems is an array using which I'm populating the sidemenu
    NSString *  str = [menuItems objectAtIndex:indexPath.row];
    NSString *jsCommand = [NSString stringWithFormat:@"execute('%@')", str];
    [self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
}

And your javascript function declaration to:

 window.execute = function (str) {...}

You will find your passed value into str function parameter.

这篇关于从本地code调用Javascript函数(IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆