将 NSString 插入 NSString

发布于 2024-10-18 03:50:00 字数 439 浏览 2 评论 0原文

我是 Objective C 的新手,有一个非常基本的问题。所以我在一个 UIViewController 中有 2 个变量(IBOutlets 是 UITextFields)。用户在这些 UITextFields 中输入文本后,他/她将进入新的视图控制器。当用户输入视图控制器 #2 时,我想将视图控制器 #1 中的 IBOutlet 值插入到 NSString I 预设中。例如,1 个变量是姓名,1 个变量是兴趣。 NSString 应该显示为“Hello [name],感谢您对 [interest] 的兴趣。我们很感激。”那么我如何将 IBOutlet 从 viewcontroller 1 传递到 viewcontroller 2,并将它们作为 NSString 插入到不可变的 NSString 中(感谢您的兴趣等部分)。我很感谢你能提供的任何帮助,因为我是一个完全 Objective C 的新手。感谢您花时间阅读本文。

——雷诺

I'm new to Objective C and have a pretty basic question. So I have 2 variables (IBOutlets that are UITextFields) in one UIViewController. After the user enters text into those UITextFields, s/he proceeds to a new viewcontroller. When the user enters viewcontroller #2, I want to insert the values of the IBOutlets from viewcontroller #1 into an NSString I preset. For example, 1 variable is a name and one is an interest. The NSString should read "Hello [name], thanks for your interest in [interest]. We appreciate it." So how do I pass the IBOutlets from viewcontroller 1 to viewcontroller 2, and insert them as NSStrings into the NSString that isn't variable (the thanks for your interest, etc. etc. part). I appreciate any help you can provide because I'm a total Objective C newbie. Thanks for taking the time to read this.

-Reynold

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2024-10-25 03:50:00

这实际上是一个由两部分组成的问题。要按照您想要的方式将这些字符串组合在一起,我建议:

[NSString stringWithFormat:FORMAT];

在您的情况下,实现可能有点像这样:

NSString *name = nameTextField.text;
NSString *interest = interestTextField.text;

NSString *resultString = [NSString stringWithFormat:@"Hello %@, thanks for your interest in %@. We appreciate it",
name,
interest];

将此值传递给另一个视图控制器有点复杂。我建议在第二个视图控制器中创建一个属性,如下所示:

@interface ViewController2 : UIViewController{
    NSString *myString;
}

@property(nonatomic, retain) NSString *myString;

@end

然后,当您设置新的视图控制器时,您可以像这样配置它:

UIViewController *vc = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

vc.myString = resultString;

[self.view addSubview:vc.view];
[vc release];

祝你好运。

This is really a two part question. To put those strings together the way you want to, I'd recommend:

[NSString stringWithFormat:FORMAT];

In your case, the implementation would probably be a little like this:

NSString *name = nameTextField.text;
NSString *interest = interestTextField.text;

NSString *resultString = [NSString stringWithFormat:@"Hello %@, thanks for your interest in %@. We appreciate it",
name,
interest];

Passing this value to another view controller is a little bit more complicated. I recommend creating a property in the second view controller like so:

@interface ViewController2 : UIViewController{
    NSString *myString;
}

@property(nonatomic, retain) NSString *myString;

@end

Then, when you set up the new view controller you can configure it like this:

UIViewController *vc = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

vc.myString = resultString;

[self.view addSubview:vc.view];
[vc release];

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文