views:

437

answers:

2

I am new to Objective C with a background primarily in database programming. I am developing an iPhone medical application that involves multiple formulas for calculations, using many variables. Essentially each formula will have its own screen, but numeric entries and calculations from each screen should appear on the screens for other formulas that share common entries, as the user navigates between screens, and variable values changed on one screen should update if appearing on another screen.

I understand the potential pitfalls of global variables, but it seems that a set of global variables is what I need. I am wondering what the best method to accomplish this goal is. I have reviewed the use of singletons on this site and in Apple's documentation, and perhaps this method is the solution. I also was wondering perhaps if delving into Core Data is the right path, saving the numeric variables entered or calculated on one screen, to be retrieved when a new formula is brought up on another screen.

Thanks in advance for any advice.

+3  A: 

The commonly accepted architecture for what you are asking is the model part of the "Model-View-Controller" design pattern.

You would define your set of calculations and the data they work upon in your model. The model consists of one or more Objective-C classes, containing the values from your computations in instance variables. The methods in your model classes do the actual math. You don't need global variables if your model gives you access to all of the related variables. The only variable you need to access is the model object itself, which could be kept in the app delegate.

The model is just an abstract description, it does not contain any user interface parts. You'll use a controller class to tie your model to user interface elements, like text fields or buttons.

See Apple's description of MVC here or here.

Nikolai Ruhe