Strong Like Bull
Strong Like Bull

Reputation: 11297

How does one communicate between view controllers in a UINavigation type application?

I have a UINavigation based application that gathers information on various screens and eventually makes a web service request using all the parameters collected.

So I have A,B,C,D view controllers. A gets the name & number, it then pushes B onto the screen with some basic info ETC ETC until it gets to D where I actually fire off the web service.

The poor method I have been using is to duplicate class fields from A onwards. Meaning if I collect name, and number, then I make those the fields of B, which then adds a few fields, and then C has class fields of both A & B which seems like a poor programing practice.

What can I do to get access to class A's fields in class D? I have gotten certain ideas but not sure how valid they are.

1). Use NSNotification (Is this overkill?) If so how do I pass fields? 2). DO I just retain all 5 view controllers and just get the info at the end? (seems very inefficient)

3). Should I just instantiate a NSObject class called Payload and just set its fields every time I bounce from one view controller to the next? (If so do I create it in class A? What if user navigates back to class A, will it then get reset ETC ETC)

As you can tell I have tried to find a solution and I am fairly new to it. Some detailed suggestions would be highly appreciated.

Upvotes: 1

Views: 883

Answers (3)

Sid
Sid

Reputation: 9536

Why not use a singleton object and pass it around?

The advantages of this method are:

  • There's only one instance whose reference is being passed around between viewcontrollers
  • Changes you make are reflected the next time you access this object from another view controller

And to answer one of your questions, NSNotification allows us to pass objects along....

Here's a good example on singleton objects in iOS by Matt Galloway. It's the one I always refer to:

http://www.galloway.me.uk/tutorials/singleton-classes/

Upvotes: 0

TheBlack
TheBlack

Reputation: 1245

Depending on situation, there are several ways that seem appropriate.

  1. Get to know MVC Design Pattern

  2. Classes are not data storage. If class doesn't have interface to interact with represented object, excluding accessors, you're doing it wrong.

3.

I have a UINavigation based application that gathers information on various screens and eventually makes a web service request using all the parameters collected.

So, your web request is based on parameters gathered from various views. Why not create an model of said request? MyRequest or something like that :) Or several more specific variants, sharing common parent class? This generator holds logic, gathers data and parameters as you advance trough views and provides NSUrlRequest at the end to WebView or maybe different kind of object which is NSURLRequest delegate and conforms to UITableViewDataSource/Delegate protocols to be used to display downloaded data.

Upvotes: 2

Desdenova
Desdenova

Reputation: 5378

I'd go for 3) and yes you should create it at the beginning (Class A).

But maybe user go back to Class A to change the value on purpose so resetting it doesn't seem to be a problem.

Upvotes: 0

Related Questions