objective-c - 方法中只有最后一行执行(Objective-C)

标签 objective-c ios xcode xcode4.3

首先,我想为我缺乏一般的编程知识而道歉。我在学习的过程中发现这个网站是对我这样做的巨大赞美。

我创建了一个包含三个 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/

相关文章:

xcode - 无法在 swiftui 中将类型 '()' 的值转换为预期参数类型 '(() -> Void)?'

ios - 应用程序在 iOS 12.0 中崩溃

ios - 在 UIView 中动态添加和居中 UIImages

ios - 停止特定的 UIView 动画 block - iOS

objective-c - 从 NSURL 设置 NSImageView 的 NSImage

ios - Facebook SDK 3.0 : how to receive user's e-mail?

objective-c - NSSortDescriptors 中的 block - Objective C

objective-c - 启动画面期间的 UIAlert

iphone - 如何检测包含目标操作(对应于按钮单击)的 "viewcontroller"文件?

objective-c - 自定义 UITableView