2

花了整个晚上谷歌搜索这个,仍然没有骰子。我只是想将字符串传递给嵌入式 html/javascript 测试文件。我没有收到任何错误,但我也没有得到调用的函数:[

这是我得到的代码明智的:

详细控制器.h

#import <UIKit/UIKit.h>

@interface DetailController : UIViewController<UIWebViewDelegate>{
    UIWebView *webView;
}


@property (nonatomic, strong) NSArray *Detailinfo;
@property (nonatomic, strong) IBOutlet UILabel *DetailName;
@property (nonatomic, strong) IBOutlet UILabel *dob;
@property (nonatomic, retain) IBOutlet UIWebView *webView;


@end

详细控制器.m

#import "DetailController.h"

@interface DetailController ()


@end

@implementation DetailController
@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];




    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton = YES;
    UINavigationItem *topBar;
    topBar.title = self.Detailinfo[0];
    self.DetailName.text = self.Detailinfo[0];
    self.dob.text = self.Detailinfo[1];


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"javascriptTest" ofType:@"html"]];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requestObj];
    self.webView.delegate = self;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"loadText(%@)", self.Detailinfo[1]]];

}





@end

和 HTML 文件 javascriptTest.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test JavaScript</title>
<script>
function loadText(txtString)
{
    var divd = document.createElement('div');
    divd.innerHTML = "<p>" + txtString + "</p>";
    document.body.appendChild(divd);
    document.getElementById('info').value = "";
}
</script>
</head>
<body>
    loaded
<!--
<input type="text" id="info" />
<button onclick="loadText(document.getElementById('info').value)"> Load Text </button> 
-->
</body>
</html>

任何帮助深表感谢 :]

更新正如@ACB所说,只需要在javascript函数中添加''@"loadText('%@')"

4

1 回答 1

2

你需要改变,

@"loadText(%@)"

@"loadText('%@')"

由于您将字符串传递给此 javascript 函数,因此它应该在引号内。

正如法比奥所说,如果字符串中有任何内容,您可能必须转义该字符串"

于 2013-02-06T18:11:43.983 回答