gpt4 book ai didi

iphone - 如何在webview中加载html文件时调用html函数?

转载 作者:行者123 更新时间:2023-11-29 13:05:38 24 4
gpt4 key购买 nike

我在从 xcode 调用 htmnl 函数时遇到问题,这里我正在编写 Html 和 objective-c 的代码,

这是html函数

<script type="text/javascript">
function load(val)
{
alert(val);
document.getElementById("h_dvid").value=val;
}

</script>

在 viewcontroller viewdidload 中,我这样调用该函数,

- (void)viewDidLoad
{
[super viewDidLoad];
web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 360)];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
[self.view addSubview:web];

NSString* str=[NSString stringWithFormat:@"load('%@')",@"hi"];

[web stringByEvaluatingJavaScriptFromString:str];

web.delegate=self;
// Do any additional setup after loading the view, typically from a nib.

由于 html 文件没有完全加载,所以我没有调用加载函数,当我点击那个按钮时我点击了一个按钮然后我调用了 html 函数然后它工作正常。但是我需要在 viewdidload 本身中调用 HTML 函数。我正在等待你的回答。

最佳答案

当您在 UIWebView 上调用 loadRequest: 时,它将开始加载 URL。这是异步过程。在此完成之前,UIWebView 不会通过调用 stringByEvaluatingJavaScriptFromString: 方法生效。

实现这个 UIWebViewDelegate方法并调用所需的 JS 函数。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString* str=[NSString stringWithFormat:@"load('%@')",@"hi"];
[web stringByEvaluatingJavaScriptFromString:str];
}

希望对您有所帮助!

关于iphone - 如何在webview中加载html文件时调用html函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18715911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com