【问题标题】:How to display the array which is in objective C to the HTML or JavaScript,to display the contents of array on a buttonclick in HTML如何将目标 C 中的数组显示到 HTML 或 JavaScript,以在 HTML 中的按钮单击上显示数组的内容
【发布时间】:2011-09-13 06:59:10
【问题描述】:

如何在 HTML 或 JavaScript 中显示目标 C 中的数组,在 HTML 中单击按钮时显示数组的内容,

以下代码是我的.m文件代码,

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Array"ofType:@"html"]isDirectory:NO]]];

       NSString *path = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"csv"];
       NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
       NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n,"]];
       for (NSString* line in lines) {

                NSLog(@"%@", line);


                }
               NSLog(@" %d", [lines count]);

    [self.webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:[NSString stringWithFormat:@"checkContents = new Array%@",lines] waitUntilDone:NO];

以下是我的HTML文件代码,

function checkContents(Lines) {
                  // alert("test...");
                  alert(checkContents[0]);
                    // alert(document.lines[0]);

                    if (typeof(checkContents)=='undefined') 
                    ....
                    else {
                        if (checkContents.length == 0) 
                        ....
                        else { 
                            for (i=0; i<checkContents.length; i++) {


                            }
                        }
                    }
                }

<body onload="checkContents()">
<div class="greybutton" onClick="javascript:checkContents()">contents</a></div>
</body>

当我运行这个程序时,它在警报中显示未定义作为输出,我是否遗漏了 HTML 文件中的任何代码或我在哪里做错了。

【问题讨论】:

    标签: javascript html objective-c arrays


    【解决方案1】:

    你可以使用 CFArrayRef

    http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFArrayRef/Reference/reference.html

    这是一个 C 类,其中 NSArray 是它的 obj-c 版本。您可以动态更改类型,因此您可以更改为它并使用发送到 javascript 的 C 类型字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 2018-08-04
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多