1

我有一个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 个不同的按钮,每个按钮的显示取决于属性是否包含某种值。

4

1 回答 1

4

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

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

于 2011-03-24T04:49:38.687 回答