javascript - 如何在 Javascript 中显示数组的内容。数组存在于 ObjectiveC 代码中

标签 javascript html objective-c xcode

如何在 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/

相关文章:

javascript - 在 CocoaScript 中使用 Javascript 库

javascript - jQuery.fadeOut() 函数内部的整数倾斜?

ios - 我用播放按钮启动一个 NSTimer,但想按停止按钮结束它,我该如何完成呢?

javascript - 使用 JS 按钮取消嵌入 html 站点中的 php 数组中的单个项目

html - CSS 从数组中选择一个随机颜色

objective-c - xmppstream 委托(delegate)方法未在我的自定义类上调用

ios - ios中的照片序列是混合的

javascript - CRC-16-IBM 实现 (JS) 不工作

javascript - 检测是否已注入(inject) Chrome 扩展内容脚本/内容脚本包含 guard

javascript - 性能问题-css vs javascript vs jquery