UIActionSheet - 未知数量的按钮,如何知道要拨打什么?

Jas*_*ien 1 iphone objective-c uikit

我有一个UIActionSheet具有不确定数量的按钮.我需要使用委托方法buttonClickedAtIndex :(或类似的东西)来决定用户单击按钮时要调用的方法.

问题是:在不同情况下,当不同的按钮出现在不同的索引时,如何确定单击哪个按钮?

一个解决方案是查看按钮的标题并对其进行操作 - 但这是丑陋的,不可本地化的,只是不好的做法.

有任何想法吗?

Bre*_*don 5

这是一个单个控制器可能显示多个工作表的情况,但您知道每个工作表上会显示哪些按钮?如果是这样,您可以使用工作表的标记属性来区分它们.

- (IBAction)showEditSheet:(id)sender {
    UIActionSheet * sheet = [[UIActionSheet alloc] initWith...];
    sheet.tag = 1;
    [sheet showInView:self.view];
}
- (IBAction)showDeleteSheet:(id)sender {
    UIActionSheet * sheet = [[UIActionSheet alloc] initWith...];
    sheet.tag = 2;
    [sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet
  clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch(actionSheet.tag) {
        case 1:
            // This is the edit sheet
            switch(buttonIndex) { ... }
            break;

        case 2:
            // This is the delete sheet
            switch(buttonIndex) { ... }
            break;

        default:
            NSAssert(NO, @"Unknown action sheet");
    }
}
Run Code Online (Sandbox Code Playgroud)