传递JavaScript数组来的UILabel - iOS设备 [英] Passing Javascript Array to UILabel - iOS

查看:201
本文介绍了传递JavaScript数组来的UILabel - iOS设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地的HTML文件JavaScript数组在我的应用程序:

I have a javascript array in a local HTML file in my app:

//js

<script language="javascript">
    test = ["First menu item","second menu item", "third menu item"];
</script>

和我想通过这个数组iOS中一个UILabel上。我怎么能去这样做?

and I would like to pass on this array to a UILabel in iOS. How could I go about doing this?

先谢谢了!

推荐答案

我可以与下面的步骤来做到这一点:

I was able to do this with the steps below:


  1. 首先,改变你的字符串数组的JavaScript在短短的一
    字符串的方法加入()

二,使用的WebView方法
     stringByEvaluatingJavaScriptFromString:来获得上述字符串

Second, use webview's method stringByEvaluatingJavaScriptFromString: to get the string above

最后,使用的NSString的方法 componentsSeparatedByString:
    改造中的字符串数组的iOS字符串。

Finally, using NSString's method componentsSeparatedByString: to transform the string in a iOS array of strings.

现在,你只需要使用这个数组作为UIPickerView的数据源。你可以看到如何做到这一点 >。

Now, you only have to use this array as datasource of your UIPickerView. You can see how to do this here.

这是如何code看起来像:

This is how to code would look like:

您的javascript:

Your javascript:

<script language="javascript">
    var test = ["First menu item","second menu item", "third menu item"];
    var testString = test.join('#');
</script>

您的Objective-C code:

Your objective-C code:

NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:@"testString"];
NSArray *testArray = [testArrayString componentsSeparatedByString:@"#"]; 
NSLog(@"%@", testArray);

下面是什么的NSLog会打印:

Here's what the NSLog will print:

2013-08-20 23:48:29.578 YourProject3633:c07] (
    "First menu item",
    "second menu item",
    "third menu item"
)

编辑:

要显示的标签阵列的内容,而不是UIPickerView,你可以做(​​未经测试,但应工作):

To show the array content in a label, instead of a UIPickerView, you can do (untested, but should work):

self.myLabel.text = [NSString stringWithFormat:%@, testArray];

或只是再次用逗号连接阵列的字符串,像这样的:

or simply joining the array's strings again with a comma, like this:

self.myLabel.text = [testArray componentsJoinedByString:@","];

我不知道是否有人有更好的解决办法,这是我想到的第一件事。让我知道,如果它帮你。

I don't know if someone has a better solution, this is the first thing I thought. Let me know if it helped you.

这篇关于传递JavaScript数组来的UILabel - iOS设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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