【发布时间】: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