0

I'm a newb to iphone development and objective c, but hoping some folks smarter than me can lend a hand. Here's my problem:

I have a view based app with about 7 different view controllers. The user navigates via a bottom tab bar. I have the users entering data in the first view controller to an object named "copies". I need to get the copies value to another controller so it can be used for calculations. This needs to be done for many objects in the apps other controllers too.

Example:

User enters Copies value in 1st view controller.
User enters Price value in 6th view controller.

7th view controller calculates copies x price = grand total

In my research I worked out the singleton method, but that seems limited to static data.

What's the best way to ensure that another view controller can access an object the user has filled in? I'm trying to avoid going a SQLite route currently. I want to stick to something basic and work my way up in complexity. Does anyone have any sample code I can review? It really helps to see how others have tackled this before.

Thanks in advance!

user390834
  • 11
  • 3

1 Answers1

0

If I've understood you correctly there is just one copies value and one price value in the whole app. If that is the case...

There are many ways to do this. Easiest way (perhaps): you could make a Singleton object of a class that you define that has copies and price as properties. Singleton in Objective-C is here, and you would define your properties within the Singleton class. Then you would just call its shared instance and use the values on that. So your code would look like this:

   [ThatCrazySingleton sharedInstance].copies = 5;

for writing.

Hope this is what you're looking for.

If you don't want to use a Singleton, at some point one of the UIViewControllers would need to send a message to the others with the copies and price values (hopefully wrapped up ["encapsulated"] in an object). This means that you have to get a reference to the other View controllers, which you can always do by going through the hierarchy.

Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • Thanks for the quick response. Yes, there is just one common object of "copies", but the value is a user input. So, I couldn't hard code it to be say, "5". It sounds like messaging may be the way to go. Do you have any references/sample code I could look at to learn more? – user390834 Jul 14 '10 at 01:20
  • It's no problem that the value is user input. You would set it on, say, a touchUpInside of a button (so the could would be `[ThatCrazySingleton sharedInstance].copies = myTextField.text;`) the TextField would be hooked up to an IBOutlet. But you should probably go back and do some basic tutorials, and then most of this might make sense. – Dan Rosenstark Jul 14 '10 at 01:25
  • Here's the problem I can't get past then. This the class where I have the singleton set MyManager.h MyManager.m This is the class where the user inputs the "copies" UITextView SixthViewController.h SixthViewController.m This is where I need the copies value to show. SeventhViewController.h SeventhViewController.m So, I would have to create a button on #6 to set a value in MyManager. It didn't work for me. If you can elaborate on what tutorials your talking about that may help. – user390834 Jul 14 '10 at 03:39
  • I really recommend the LeMarche (Apress) beginning iphone dev book to get your started. – Dan Rosenstark Jul 14 '10 at 03:58
  • Thanks for the reference. I bought the book and will give it a good read. In the meantime I'm going to keep tinkering. – user390834 Jul 14 '10 at 13:23