Global variables in Objective C on iPhone

ぃ、小莉子 提交于 2019-12-24 06:04:45

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1643467/global-variables-in-objective-c-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!