2011-02-13 7 views
0

verbIn私は(適切なプロパティを持つ)ボタンアクションは言葉を交換する必要があるプロパティthePlace、theVerbとのtheNumber逃しライン

にテキストを供給し、テンプレート内の三つのフィールドを持っているチュートリアル。

これはアクションです:

-(IBAction) createStory:(id)sender{ 

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

問題はそれだけで(すなわち、この場合にのみ単語動詞はtheVerbの内容によって置き換えられる置換の最後に作用するということである

私はアクションに変更した場合:(thePlaceコンテンツに)

-(IBAction) createStory:(id)sender{ 

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

} 

のみワード場所が交換されるの

最後の置換だけが実行され、最初の2つが無視されるのはなぜですか?何か案は?

答えて

0
theStory.text =[theTemplate.text 
       stringByReplacingOccurrencesOfString:@"<number>" 
       withString:theNumber.text]; 
theStory.text =[theTemplate.text 
       stringByReplacingOccurrencesOfString:@"<place>" 
       withString:thePlace.text]; 

コードでは、テンプレートの文字列が変更され、Story.textに割り当てられます。

コードでは以前に変更されたテキストは使用されません。テンプレートのテキストを使用します。二行目は、あなたが戻って最初の行から得たテキストを変更します。この場合

theStory.text =[theTemplate.text 
       stringByReplacingOccurrencesOfString:@"<number>" 
       withString:theNumber.text]; 
theStory.text =[theStory.text 
       stringByReplacingOccurrencesOfString:@"<place>" 
       withString:thePlace.text]; 

たぶん、あなたはこのような何かを試してみてください。

+0

@fluchtpunktというお返事ありがとうございます。それは理にかなっているようです。私はそれを試してみましょう。 –

+0

はい。今はうまくいく。あなたの助けてくれてありがとう@fluchtpunkt –