Tattat
Tattat

Reputation: 15778

How to pass a value from one view to another? (iPhone)

for example, I have a textfield, that records the user name, after I click the button, it will display the next view, and I want the next view ge the text field data, and display on the second view, how can I do so?

Upvotes: 0

Views: 4248

Answers (4)

Jordan
Jordan

Reputation: 21770

Here's a great example of how to pass a variable (using MVC) from one controller (detail) to the next (editing), from Apple.

CoreDataBooks Example

In Detailed View (pass Book to EditingViewController):

 EditingViewController *controller = [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];

        controller.editedObject = book;
         ...

        [self.navigationController pushViewController:controller animated:YES];
        [controller release];

In the Editing View (EditingViewController):

- (IBAction)save {
    ...
        [editedObject setValue:datePicker.date forKey:editedFieldKey];
        [editedObject setValue:textField.text forKey:editedFieldKey];
    }

    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163308

As a general rule, you'd want to create properties of your view controller to receive data that will populate it's view. Therefore, say your text field was a property, you'd write:

myViewController.text_field.text

where myViewController is the view controller that you are about to show to the user.

Upvotes: 2

philsquared
philsquared

Reputation: 22493

Sounds like you're not following MVC conventions.

If you have a seperate set of "model" classes, that just hold the data, your first view would update it with changes from the text view (either as you go, or when you leave the view). The second view would get its data from the model - so it would get the updated field.

If you have multiple views referencing the same model "live" at the same time you might need to look at key-value coding too.

Upvotes: 5

Tuomas Pelkonen
Tuomas Pelkonen

Reputation: 7831

Pass

text_field.text

to the second view in any way you like

Upvotes: 0

Related Questions