0

I have two view's in my app. In the first view I have a table view which displays data downloaded from the Internet. The FirstViewController has a method to get the data and update the view:

- (void)viewDidLoad
{
    // Create PlanGenerator
    _planGenerator = [[PlanGenerator alloc] init]

    [self loadPlan];

- (void)loadPlan
{
    _plan = [_planGenerator getData]

    // Updating the view

    // Updating the table view
    [self.tableView reloadData]
}

To download the data from the internet I have a class called PlanGenerator. This class has an instance method called getData, it returns an NSArray. The table view uses the instance variable _plan (array) to display data in the table view.

In the second view (controlled by the SecondViewController) you can make some adjustments on what to download. To tell these change the PlanGenerator I used the concept of class properties. Now when I changed something in the second view (actually it's just one parameter) I want to call the method loadPlan from the FirstViewController.

My first thought was to create a class method, but then I would have to creat "class properties" for every variable the method uses.

Is there an easier way to do this?

Community
  • 1
  • 1
Codey
  • 1,131
  • 2
  • 15
  • 34
  • A couple of things.... When you say "second view", do you actually mean second view controller? (There's a **big** difference.) Are you aware that `alloc/init` creates a **new** object which won't have information that you've given to some other `PlanGenerator` in some other piece of code? – Phillip Mills Sep 20 '14 at 14:07
  • Yes I know that this is a big difference, but I thought it was obvious that any View has also a ViewController. Anyway I added that in my question. But I don't really understand what you mean with the second part of your question. I know that every new object of `PlanGenerator` is different, but the data that is returned by `getData` is the same for every instance. – Codey Sep 20 '14 at 15:36
  • Some people have multiple views in a single controller and hide/show them as necessary. (*shrug*) I see you've changed your question so that the `PlanGenerator` isn't a local variable. That helps with the second part of my comment; I was talking about how you were going to call `loadPlan` on something that would go out of scope at the end of the method. – Phillip Mills Sep 20 '14 at 15:41
  • Ok now I understand, yes the _planGenerator and _plan (array) are instance variables. – Codey Sep 20 '14 at 15:45

1 Answers1

0

You are missing some basics.

Try this design, assuming that FirstVC is used to display data and has tableview. SecondVC (your PlanGenerator) is used to get/download data.

In SecondVC:

  1. Create whatever property(parameter, etc. says criteria) that FirstVC will supply to decide what to download.

  2. Create a public method getData.

In FirstVC:

  1. Create an iVar (says _myPlanGeneartor) and allocate it.

Now from the instance of FirstVC, you have access to an instance of SecondVC (_myPlanGenerator). With that you can supply criteria parameter and request data (getData).

user523234
  • 14,323
  • 10
  • 62
  • 102
  • Oh I'm sorry, I think I didn't express myself well. I have 2 ViewControllers (in the MVC pattern the Controllers) and I have the PlanGenerator class (in the MVC pattern the Model). That's actually my structure. – Codey Sep 20 '14 at 15:28
  • Do you have another idea? – Codey Sep 22 '14 at 20:38