1

このコード(.mファイル内)を使用して、バンドルされたjavascriptファイルに変数を渡します。

[animalDesciption stringByEvaluatingJavaScriptFromString:@"var myIDcounterVariable = '1'"];

これを設定すると、UIButtonを押してUILabelの値を変更できます。

-(IBAction)next:(id)sender {
number++;
[currentNumber setText:[NSString stringWithFormat:@"%d", number]];

}

Labelで行っているように、ボタンでJavaScript変数の値を変更するにはどうすればよいですか?

私のつまずきの言葉を許してください!私はこの種のプログラミングにとても慣れていません...

さらに情報が必要な場合は教えてください。

4

2 に答える 2

0

必要なのはこれだけですか?

-(IBAction)next:(id)sender {
    number++;

    // You want Javascript like this to run:
    //     myIDCounterVariable = '1'
    // but with the current value of 'number' in it.

    // Construct the string:
    NSString* jsString = [NSString stringWithFormat:@"myIDCounterVariable = '%d'", number];

    // and run it
    [animalDesciption stringByEvaluatingJavaScriptFromString:jsString];
}
于 2012-04-15T01:01:47.487 に答える
0

私がやったことは、このJavaScript関数をUIWebViewに追加することでした。

<script type="text/javascript">
var max = 100;

function goToNext() {
    var hash = String(document.location.hash);
    if (hash && hash.indexOf(/hl/)) {
        var newh = Number(hash.replace("#hl",""));
        (newh > max-1) ? newh = 0 : void(null);
        document.location.hash = "#hl" + String(newh-1); 
    } else {
        document.location.hash = "hl1";        
    }
}

</script>

次に、次のようにIBActionを使用してこのJavaScript呼び出しを送信します。

- (IBAction)next:(id)sender {
    [animalDesciption stringByEvaluatingJavaScriptFromString:@"goToNext()"];
}
于 2012-04-15T16:45:29.927 に答える