0

I know one type that is segue,and now like to try another methods to pass data between two viewControllers. If anyone know, please help me to do.

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
  • 4
    Check this http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Kumar Jul 22 '15 at 10:09

1 Answers1

0

First of all: This kind of Q has a problem: What are two (different) ways? What ways are equivalent so count as 1. For example I would consider setting a property in the destination using dot-notation or using explicit setters are the same way, even the code is not bitwise identical.

However, …

A. Setting isolated properties

When the destination view controller (dvc) needs some data, it is obvious that it can be passed by setting a property:

dvc.property1 = …;
dvc.property2 = …;

To me it is only one way independent of the way, the property is set. (Dot notation, KVC, object subscription, explicit setter, $whatever.) Furthermore for me it is still the same way, when you get the dvc from a segue or when you instantiate yourself: The core that makes all the ways equivalent is that the source view controller explicitly sets all data needed by the dvc.

DVC* dvc = segue.destinationViewController;
dvc.property1 = …;
dvc.property2 = …;

… or …

DVC* dvc = [[DVC alloc] initWithNib:… bundle:…];
dvc.property1 = …;
dvc.property2 = …;

I know that others say, this are two ways. I do not think so. Passing the data is equivalent, even getting the view controller is different. However, count an extra +1 or not.

What is more important to me by far, is the dependencies you get. In this way the dvc determines the set of properties to pass. The svc depends on the API of the dvc. Any change of this set in dvc causes a change in svc. Please take into account that maybe the dvc does not need data itself, but has to know it to pass it to a third view controller, let's call it 3vc (i. e. Overview -> Details -> More Details).

In this scenario DVC is highly reusable, because it has no dependency, but SVC heavily relies on the API of DVC.

B. Setting a data set

For me a different way is, to set a data set instance, probably a model object, independently of the properties used by dvc.

DVC* dvc = segue.destinationViewController;
Data *data = [Data new];
data.property1 = …;
data.property2 = …;
dvc.data = data;

On a first view this looks like (A) with some additional work for the Data instance. But it isn't. An example: You have a list of persons. Every person has a list of e-mail addresses. The svc shows the persons. You can go to a detail view showing the person's details including one e-mail address. you can get to a 3vc to see a list of all e-mail addresses.

In such an application you already have classes being your model, i. e. Person and eMailAdress. Simply passing the selected person to the dvc, the dvc can pass the same person to the 3vc. You simply pass an entry point to your model and every following view controller picks out the data it needs, following the relations in your model.

DVC* dvc = segue.destinationViewController;
Person *person = …;
dvc.person = person;

The API of the dvc will change in such a case rarely. There is a formal dependency between svc and dvc and the data provider, but this dependency doesn't hurt because of the low frequency of changes. Moreover if the model changes, you only have to change the view controller that deals with this change, what is quite natural. But you do not need to change svc, because dvc needs additional data after a change of Person. Changes are kept locally.

C. Making the source view controller a data provider

Another way is simply to use the svc as an data provider. The dependency are similar to (A), because the dvc determines the API, in this case offered by the svc. So there is a technical difference that can be useful if computation of the data to pass is slow and the data is not always needed. It allows you to be more lazy.

D. Use a global object

ehm

/* Intentionally left blank */

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50