2

これをグーグルで一晩中過ごしましたが、まだサイコロはありません。埋め込まれた html/javascript テスト ファイルに文字列を渡そうとしています。エラーは発生していませんが、次のいずれかの関数も取得していません:[

コードに関して私が得たものは次のとおりです。

DetailedController.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

DetailedController.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 関数に文字列を渡すため、引用符で囲む必要があります。

"Fabio が言ったように、文字列が内部にある場合は、文字列をエスケープする必要があるかもしれません。

于 2013-02-06T18:11:43.983 に答える