我在从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功能,那么它的工作fine.BUT我需要调用HTML功能viewdidload
本身。我等着你的回答。
当你调用loadRequest:
上UIWebView
,将开始加载URL。这是异步过程。直到此完成,UIWebView
通过调用stringByEvaluatingJavaScriptFromString:
方法才有效。
实现此UIWebViewDelegate
方法并调用所需的JS函数。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString* str=[NSString stringWithFormat:@"load('%@')",@"hi"];
[web stringByEvaluatingJavaScriptFromString:str];
}
希望对您有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句