开发者

Passing Non-Static Objects Between View Controllers

开发者 https://www.devze.com 2023-01-07 05:51 出处:网络
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'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 be开发者_如何学运维st 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消