开发者

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

开发者 https://www.devze.com 2022-12-22 11:26 出处:网络
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, h

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开发者_StackOverflow do so?


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.


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.


Pass

text_field.text

to the second view in any way you like


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

精彩评论

暂无评论...
验证码 换一张
取 消