【发布时间】:2010-09-19 12:39:26
【问题描述】:
我是新手,所以请原谅我可能很简单的问题。希望你能很容易地帮助我!我正在制作一个应用程序,它使用 4 个变量来计算推荐给用户的水量。这个 switch 语句的问题在于,无论我将 age 设置为什么值,它总是执行最后一种情况。这是为什么呢?
另外,由于我的变量都是从不同的(IBAction)方法获得的,是否需要将它们定义为全局变量?我该怎么做呢?
请帮忙!
非常感谢:)
-(IBAction) updatePrefs:(id) sender
{
switch (age){
case 1:
RWI = 1;
Output.text = [NSString stringWithFormat:@"You should drink 1 litre a day"];
case 2:
Output.text = [NSString stringWithFormat:@"You should drink 1.5 litres a day"];
case 3:
RWI = (weightkg * weightpounds * activity);
Output.text = [NSString stringWithFormat:@"You should drink 2 litres a day"];
break;
default:
break;
}
【问题讨论】:
-
年龄变量从何而来?是财产吗?你有@synthesize 吗?
-
不,我刚刚在我的 .h 类文件中声明了它 @implementation AppPrefsViewController int age; UILabel *输出;它后来在另一个方法中给出了一个值
-
为什么你使用
[NSString stringWithFormat:@"You should drink 1 litre a day"]而不是@"You should drink 1 litre a day"?
标签: xcode variables global switch-statement