Florian Schaal
Florian Schaal

Reputation: 2660

Best way to pass data between viewcontrollers (Cache data) in MonoTouch

So Im rewriting an App that I have previously previously written in objective-c. In objective-c I would store the data(5 array's) in separate NSUserdefaults and then call them in the new ViewController.

Since I started programming in MonoTouch this feels kinda wrong. Im also now using shared code for this and created a class that is fetching the data. Let me explain how the flow of my app works:

  1. The user enters a number into a text field.
  2. Now this number is send to the class which will fetch the data and filter it. Ones that is complete it will return 5 array's / Lists.
  3. Now I somehow have to save or pass the data and push to the next ViewController where the data will be displayed in Labels.

So what would be the best way to tackle this?

Upvotes: 4

Views: 4362

Answers (1)

NovaJoe
NovaJoe

Reputation: 4625

Subclass the ViewController, adding some new parameter(s) on it's constructor for the data, so that you can pass the data to the controller when you display it.

public class MyViewController : UIViewController
{
    private MyData _myData;

    public MyViewController(MyData myData)
    {
        _myData = myData;
    }
}

Then use it:

(assuming we're already in another view controller that has a NavigationController):

var myViewController = new MyViewController(myData);
this.NavigationController.PushViewController(myViewController, true);

or (as a "modal")

var myViewController = new MyViewController(myData);
this.PresentViewController(myViewController, true);

Upvotes: 2

Related Questions