Insert NSStrings into NSStrings
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
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.
精彩评论