1

まず、プログラミング全般に関する知識が不足していることをお詫びしたいと思います。私は学習の途上にあり、このサイトが私にとって大きな褒め言葉であることを発見しました。

3つのUITextField(thePlace、theVerb、theOutput)と2つのUITextViewを使用してプログラムを作成しました。このプログラムでは、一方のtextview(theOutput)がもう一方のtextview(theTemplate)からテキストを取得し、一部の文字列をテキストフィールドに入力されたテキストに置き換えます。

ボタンをクリックすると、以下にリストされているメソッドcreateStoryがトリガーされます。これは、1つの例外を除いて正常に機能します。出力のテキストは、文字列'number'をテキストフィールドのテキストに変更するだけです。ただし、メソッドの順序を変更して「place」、「number」、「verb」を置き換えると、動詞のみが変更され、番号は変更されません。

これはある種の簡単な修正だと確信していますが、見つかりません。トラブルシューティングを手伝ってくれる人がいますか?

- (IBAction)createStory:(id)sender {
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text];
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text];
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text];
}

どうもありがとう//エミル

4

3 に答える 3

2

theOutput.text問題は、各行の内容を上書きしていることです。var1 = var2;のデータを上書きvar1し、の内容に置き換えますvar2

これを試して:

- (IBAction)createStory:(id)sender 
{
   NSString* tempStr = [theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text];
   tempStr = [tempStr stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text];
   theOutput.text = [tempStr stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text];
}

これは意味がありますか?:)

于 2012-06-04T22:42:57.657 に答える
0
- (IBAction)createStory:(id)sender {
    theOutput.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text];
    theTemplate.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text];
    theTemplate.text=[theTemplate.text stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text];
}

これは機能しますか?

于 2012-06-04T22:38:54.163 に答える
0

まず、空の値を返さないことthePlace.textを確認しますか?theVerb.textしかし、これはあなたの問題ではありません。

NSLog(@"thePlace: %@",thePlace.text);
NSLog(@"theVerb: %@",theVerb.text);

コードは次のようになります。

- (IBAction)createStory:(id)sender {

    NSString * output = theTemplate.text;

    output = [output stringByReplacingOccurrencesOfString:@"<place>" withString:thePlace.text];
    output = [output stringByReplacingOccurrencesOfString:@"<verb>" withString:theVerb.text];
    output = [output stringByReplacingOccurrencesOfString:@"<number>" withString:theNumber.text];

    theOutput.text = output;
}
于 2012-06-04T22:40:34.833 に答える