iPhone/Objective-C UIActionSheet,带有以编程方式添加的按钮

发布于 2024-10-27 00:33:33 字数 1716 浏览 5 评论 0原文

我有一个 UIActionSheet,其中包含根据属性是否包含值以编程方式添加的按钮。

这是我当前正在使用的代码:

- (IBAction)infoButtonTap:(id)sender {
    MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];

    if (obj != nil) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Details"
                                                                 delegate:self
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];

        // Programatically add Other button titles if they exist.
        if (obj.firstProperty) {
            [actionSheet addButtonWithTitle:@"Dosomething"];
        }

        if (obj.secondProperty) {
            [actionSheet addButtonWithTitle:@"Dosomethingelse"];
        }

        [actionSheet addButtonWithTitle:@"Static button"];

        actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

        [actionSheet showInView:self.view.window];
        [actionSheet release];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];

    switch (buttonIndex) {
        case 0: // Dosomething
            break;
        case 1: // Dosomethingelse
            break;
        case 2: // Static button
            break;
        default:
            break;
    }
}

问题是,我如何解释其中一个属性可能为 null 的事实并在 clickedButtonAtIndex: 中处理该问题?索引值将会改变。我有大约 4 或 5 个不同的按钮,每个按钮的显示取决于属性是否包含某种值。

I have a UIActionSheet that contains buttons that are programmatically added depending on whether a property contains a value or not.

This is the code I'm currently using:

- (IBAction)infoButtonTap:(id)sender {
    MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];

    if (obj != nil) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Details"
                                                                 delegate:self
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];

        // Programatically add Other button titles if they exist.
        if (obj.firstProperty) {
            [actionSheet addButtonWithTitle:@"Dosomething"];
        }

        if (obj.secondProperty) {
            [actionSheet addButtonWithTitle:@"Dosomethingelse"];
        }

        [actionSheet addButtonWithTitle:@"Static button"];

        actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

        [actionSheet showInView:self.view.window];
        [actionSheet release];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];

    switch (buttonIndex) {
        case 0: // Dosomething
            break;
        case 1: // Dosomethingelse
            break;
        case 2: // Static button
            break;
        default:
            break;
    }
}

The problem is, how do I account for the fact that one of the properties may be null and handle that within the clickedButtonAtIndex:? The index values will change. I have around 4 or 5 different buttons, each displayed depending on whether a property contains a value of some sort.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南风起 2024-11-03 00:33:33

最简单的方法可能是使用 UIActionSheetbuttonTitleAtIndex: 方法来获取按钮标题并使用它来确定要执行的操作。

或者,您可以使用类似于添加按钮的逻辑来弄清楚索引的确切含义。

The simplest way is probably to use the buttonTitleAtIndex: method of UIActionSheet to get the button title and use this to determine what action to take.

Alternatively you could use logic similar to where you add the buttons to figure out exactly what the index means.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文