Passing variables to different view controllers

2020-03-30 03:07发布

问题:

I've searched and searched but nothing has really worked.

I'm trying to set a textvalue from a text box, into a string or whatever, so that I can call it up later in a different view controller. I can't seem to get it to work!

I'd also like numbers to be carried over, such like currency's.

Any ideas on them?

Cheers.

回答1:

You could make an instance variable on the other view controller retain or copy the value before you push/pop the view. For example:

OpenNextViewController *varNextPageController = [[OpenNextViewController alloc] initWithNibName:@"OpenNextViewController" bundle:nil];
varNextPageController .textString= self.textString;
[[self navigationController] pushViewController:varNextPageController animated:YES];
[varNextPageController release];

In "OpenNextViewController" in this example have an instance variable "textString" that retains or copies (depending on your needs) your text.



回答2:

Spend some time trying to grok the Model View Controller pattern.

In your case you may be looking to share data between different views sharing a common Model. The Model is the store of your data, in your case the textvalue.



回答3:

Your question is a bit vague. Could you give any more specifics. It sounds like you want to know:

  1. How to get values from controls. In the case of a text field there should be a text property you can get the value from.

  2. How to share values between controllers. Not sure exactly what you mean. The controller usually orchestrates the sharing of values between different views by using a model as the authoritative version of the data.

Again, if you can be any more specific we may be able to help more



回答4:

If you want it in several controllers, then I would think you need to run it through the model?



回答5:

Do you guys think that using the AppDelegate as the holder for one's model is fundamentally wrong? I mean AppDelegate is easily visible to all controllers so it's easy to bind to and get/set it's properties.

pom



标签: cocoa-touch