iPhone上Objective C中的全局变量 [英] Global variables in Objective C on iPhone

查看:164
本文介绍了iPhone上Objective C中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C的新手,主要背景是数据库编程。我正在开发一个iPhone医学应用程序,它涉及多个计算公式,并使用许多变量。基本上每个公式都有自己的屏幕,但是每个屏幕上的数字条目和计算应该出现在屏幕上,用于其他共享公共条目的公式,因为用户在屏幕之间导航,并且在一个屏幕上更改的变量值应该更新,如果出现在另一个屏幕上屏幕。



我了解全局变量的潜在缺陷,但似乎我需要一组全局变量。我想知道实现这个目标的最佳方法是什么。我已经在本站和Apple的文档中回顾了单身人士的使用,也许这种方法就是解决方案。我还想知道,如果钻研核心数据是正确的道路,保存在一个屏幕上输入或计算的数字变量,以便在另一个屏幕上显示新公式时获取。



感谢您提供任何建议。

模型 - 视图 - 控制器设计模式。



您可以定义您的模型集中的计算和数据。该模型由一个或多个Objective-C类组成,其中包含实例变量中的计算值。模型类中的方法会执行实际的数学运算。如果您的模型允许您访问所有相关变量,则不需要全局变量。您需要访问的唯一变量是模型对象本身,它可以保存在应用程序委托中。



该模型只是一个抽象描述,它不包含任何用户界面部分。您将使用控制器类将您的模型绑定到用户界面元素,如文本字段或按钮。



请参阅Apple对MVC的描述 here here 。 p>

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.

解决方案

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.

这篇关于iPhone上Objective C中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆