Obj-C Switch 语句整数变量 iPhone

发布于 2024-10-19 15:21:47 字数 1118 浏览 7 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2024-10-26 15:21:47

为什么不使用 if 语句而不是 switch 语句?您可以使用 cancelButtonIndex 属性来检测按下取消按钮。

if (buttonIndex == actionSheet.cancelButtonIndex) {
    // cancel button has been pressed
    // do stuff
}

此外,您还可以使用标题字符串来比较按钮。但是,按钮标题可能已本地化。当心。

NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Cancel"]) {
    // cancel button has been pressed
    // do stuff
} else {
    //
}

Why don't you use if-statement instead of switch-statement? You can use cancelButtonIndex property for detecting pressing cancel button.

if (buttonIndex == actionSheet.cancelButtonIndex) {
    // cancel button has been pressed
    // do stuff
}

Also, you are able to use title string for comparison of buttons. However, button titles might be localized. Be careful.

NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Cancel"]) {
    // cancel button has been pressed
    // do stuff
} else {
    //
}
心房的律动 2024-10-26 15:21:47

正如您所发现的,只有常量可以跟在“case”后面。在 switch 语句中与变量进行比较的值必须在编译时已知。

要获得您想要的功能,请使用 if/else 结构。就这么简单。 ;)

编辑:

并且,,这将不会编译(由于我之前提到的):

int n = 4;
const int a = someVariable;
switch (n) {
    case a:
        printf("hi");
        break;
}

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):

int n = 4;
const int a = someVariable;
switch (n) {
    case a:
        printf("hi");
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文