views:

240

answers:

4

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?

A: 

Pass

text_field.text

to the second view in any way you like

Tuomas Pelkonen
but the "textfield" is in the first view, how can I get access this value in the next view?
Tattat
I think Tuomas's point, like some of these other answers, is that rather than the new view try to pull the information from the first, that you *push* the information into the new view before displaying it. The original view knows both the string in the field and the identity of the new view, so it can connect them.
Sixten Otto
+5  A: 

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.

Phil Nash
It seems that do many coding on such a sample thing.
Tattat
Well one way or another your views will have to know about each other. That's not a good sign. In intermediate "view" needn't be heavyweight. Could just be a property on the controller
Phil Nash
I added a @interface Name : NSObject { NSString name; }@property(readwrite) NSString name;How can I get use it? I tried to [Name name:@"myName"]; but it is not working.
Tattat
I'm not sure if it's just a typo when copying here, but that should be `NSString*` in both cases (note the `*`). You'll also need method implementations for the properties, or a `@synthesize` expression. If none of this means much to you we're probably not going to solve it here.
Phil Nash
+2  A: 

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.

Jacob Relkin
A: 

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];
}
Jordan