views:

74

answers:

1

I'm pretty new to OOP in general and just really started working with Obj-c a few months back. So, please be gentle! I appreciate your help in advance. Now to the question!

I have 3 Text Fields where a user inputs name, phone, and email.

I collect and place them in a label using an NSString like this [this is the one for the name]:

- (IBAction)changeGreeting:(id)sender {
self.name = textInput.text;

NSString *nameString = name;
if([nameString length] == 0) {
    nameString = @"I Forgot";
}
NSString *greeting = [[NSString alloc] 
                      initWithFormat:@"Hello, my name is %@! Really!", nameString];
label.text = greeting;
[greeting release];
} 

With this I have been able to place the text from text.input into my label (as stated in label.text = greeting;)

I have another view where I'd like to have someone review this information (view a label too). I need to have access to name or Textinput.text in that other view.

How can I accomplish this?

+1  A: 

If you don't need to communicate changes between the two view controllers, you may want to pass it in using a custom init method. This may be best for a confirmation screen, where the prompt would make no sense without this string.

- (id)initWithFrame:(CGRect)aRect username:(NSString*)aName {
    if((self = [super initWithFrame:aRect])) {
        _myName = [aName retain];
    }
    return self
}

Another option is to implement a method on the first view controller and call it from the second.

- (NSString*)enteredUsername {
    return _myName;
}
Justin
Thanks nonomic, that's helpful! Hrm, I think I'm approaching this wrong, though. I'll want to keep all these values when the user quits the application, too (to call later if need be). I'm guessing I'm going to need to study the NSDictionary classes? Any better suggestions?
Macness
I'd recommend reading up on `NSUserDefaults`, which is basically a dictionary that persists between application launches.
Justin
It's also worth mentioning that the accessor is redundant because `name` is declared as a property. I've edited my answer to match.
Justin