【发布时间】:2011-02-28 02:06:03
【问题描述】:
我正在尝试使用 switch 语句来读出在 UIActionSheet 中单击了哪个按钮(为 iPhone 编程)。
以下是在我的 FirstViewController.m 中:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
int countarray=[areaselectArray count];
switch (buttonIndex) {
case countarray: //cancel button has been pressed
//do stuff
break;
default:
area = [areaselectArray objectAtIndex:(buttonIndex)];
[[NSUserDefaults standardUserDefaults] setObject:area forKey:@"mulValue"];
[areaselectArray release];
NSLog(@"Released areaselectArray");
break;
}
}
UIActionSheet 的按钮是从我之前构建的(尚未发布)的数组构建的。我使用
将我的取消按钮放在列表的末尾int countarray=[areaselectArray count];
[areaselect.cancelButtonIndex = countarray;]
之前在分配我的 UIActionSheet 时。由于按钮的数量根据数组中的条目数量而变化,我希望“取消”按钮简单地关闭 UIActionSheet,但在所有其他情况下,让 Switch 语句将单击按钮的值写入“mulValue” " 在标准用户默认值中。
有没有办法做到这一点?当然,我现在的主要问题是 switch 函数不会使用变量(例如我的示例中的 countarray )。在进入 switch 语句之前,有什么方法可以将值写入常量(?)?
提前致谢!
【问题讨论】:
标签: iphone objective-c switch-statement uiactionsheet