简体   繁体   中英

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

例如,我有一个文本字段,记录用户名,单击按钮后,它将显示下一个视图,并且我想让下一个视图显示文本字段数据,并在第二个视图上显示,我该怎么办所以?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM