首先,我想为我缺乏一般的编程知识而道歉。我在学习的过程中发现这个网站是对我这样做的巨大赞美。
我创建了一个包含三个 UITextField(thePlace、theVerb、theOutput) 和两个 UITextView 的程序,其中一个 TextView (theOutput) 从另一个 TextView (theTemplate) 中获取文本,并用在文本字段中输入的文本替换一些字符串.
当我单击一个按钮时,它会触发下面列出的方法 createStory。除了一个异常(exception),这工作正常。输出中的文本仅将字符串“数字”更改为文本字段中的文本。但是,如果我更改方法中的顺序以替换“地点”、“数字”、“动词”,则只会更改动词,而不是数字。
我确定这是某种简单的修复方法,但我找不到它。你们中的一些人可以帮助我解决问题吗?
- (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];
}
非常感谢 //埃米尔
最佳答案
问题是您要用每一行覆盖 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];
}
这有意义吗? :)
关于objective-c - 方法中只有最后一行执行(Objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889349/