如何在 Javascript 中显示数组的内容。数组存在于 ObjectiveC 代码中,
以下是Objective C代码,
[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]);
那么如何将此 .m 文件链接到 Html 或 Javascript,以便我可以在按钮的 OnClick 上显示数组的内容。这个问题可能很简单。但我是 javascript 和 Html 的新手,所以我不知道使用什么标签或代码来链接 .m 文件和 Html 文件。
最佳答案
如果你不懂 HTML 和 JavaScript,这真的超出了这个问题,你必须同时学习这两个,然后再回到 Cocoa。
一旦你了解了两者(并且你已经学会了 JavaScript 中的 dom 操作),你就可以使用类似这样的东西来做你想做的事情:
function showArrayContents( array ) {
// do something in JavaScript here to show the array
}
在 Objective-C 上,你可以这样调用这个函数:
[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,"]];
// this is where you call the JS function above
WebScriptObject * scriptObject = [ webView windowScriptObject ];
[scriptObject callWebScriptMethod: @"showArrayContents" withArguments: [NSArray arrayWithObject: withArguments ]]
关于javascript - 如何在 Javascript 中显示数组的内容。数组存在于 ObjectiveC 代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305892/