I have four viewControllers in my current design and I am designing an app to sell a product.
FirstViewController
gets the product image and when user clicks to the next button then it takes user to the secondviewcontroller
where user describes the product and then user clicks next button which takes user to the thirdViewcontroller
where price and condition are entered. In the lastviewcontolller
there is a post button to send the product info to the server. I am using POST
method.
The following segue approach does not fit into what I want, because it sends the firstviewcontroller
object (product image) to the secondviewcontoller
, and then secondviewcontroller
also should forward the product image to the thirdviewcontoller
and so on. I do not think it is a feasible way of doing it.
I wonder what is the best way of collection information from the first page till to the last page and send it. What is best way of handling that issue? I am using segue between the viewcontrollers.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"isSecond"])
{
// Get reference to the destination view controller
SecondViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyProductImage:productImage];
}
}
One way of doing this would be that you create a mutable dictionary (or a custom object with variables) in the first view controller. Then you would pass a weak reference to second/third/fourth view controllers of the mutable dictionary/object from first view controller. Each view controller would be able to set data to the dictionary/object and the last one would be able to process the information.
Another way would be to create a simple singleton class with variables that you want to store. The first view controller would reset the singleton variables. Then let each view controller access the singleton and store their values there, last view controller would process values from singleton.
It depends how many data you are collecting and what you personally prefer.
Please don't use a singleton, even if the majority of users here tells you so. It would violate the SOLID-Principles for several reasons.
Instead just pass the object from ViewController to ViewController.
If all ViewController expect the same model class, you can create a common base class that has the property for the model.
it could have this method
I created an example project: https://github.com/vikingosegundo/ProductWizard
Note, that all view controller derive from
ProductAwareBaseViewController
This ViewController knows how to pass the model data of class
Product
to other instances ofProductAwareBaseViewController
and subclasses of it.All other view controller don't deal with passing the data, just adding each portion of data (name, description, price) to the model and displaying it.
i.e:
Create an object to act as your application's data model. It can be a singleton or it can be a normal object that's available from a known location...such as owned by the app delegate.
Update your model when you have new information and read from the model when you need to display something. Using
prepareForSegue:
and linking controllers may be acceptable for simple things but it really doesn't scale well.