Obj-C Switch 语句整数变量 iPhone
我正在尝试使用 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 语句之前将值写入常量(?)?
提前致谢!
I'm trying to use a switch statement to read out what button has been clicked in a UIActionSheet (programming for iPhone).
The following is in my 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;
}
}
The buttons for the UIActionSheet are built from an array that I built earlier (and have not yet released). I'm placing my Cancel button at the end of the list by using
int countarray=[areaselectArray count];
[areaselect.cancelButtonIndex = countarray;]
earlier on when allocating my UIActionSheet. Since the amount of buttons changes depending on the amount of entries in the array, I'd like the "Cancel" button to simply dismiss the UIActionSheet, but in all other instances have the Switch statement write the value of the clicked button to "mulValue" in standardUserDefaults.
Is there any way to do this? My main issue right now of course is that a switch function won't take a variable (like countarray in my example). Is there any way to write the value to a constant (?) before entering the switch statement?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用 if 语句而不是 switch 语句?您可以使用 cancelButtonIndex 属性来检测按下取消按钮。
此外,您还可以使用标题字符串来比较按钮。但是,按钮标题可能已本地化。当心。
Why don't you use if-statement instead of switch-statement? You can use cancelButtonIndex property for detecting pressing cancel button.
Also, you are able to use title string for comparison of buttons. However, button titles might be localized. Be careful.
正如您所发现的,只有常量可以跟在“case”后面。在 switch 语句中与变量进行比较的值必须在编译时已知。
要获得您想要的功能,请使用 if/else 结构。就这么简单。 ;)
编辑:
并且,不,这将不会编译(由于我之前提到的):
As you've discovered, only a constant can follow 'case'. The values with which a variable will be compared in a switch statement must be known at compile time.
To gain the functionality you desire, use an if/else construct. Simple as that. ;)
edit:
And, no, this will not compile (due to what I mentioned previously):