iOS8中UIActionsheet代理方法被调用两次?

9
在iOS8中,UIActionSheet的委托方法会被调用多次。
  - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

我已经检查了配有IOS 8的iPad 2

7个回答

5

这是iOS 8的一个bug。

如@rob所说,UIActionSheet在iOS 8中已经过时了(UIActionSheetDelegate也已过时)。

要在iOS 8及更高版本中创建和管理操作表,请使用UIAlertController


2
iOS 8.0.x肯定是自iOS 4.0以来最容易出现故障的iOS版本。 - Epaga

4

我确实在几个地方看到了这种现象。它似乎是一个bug,希望很快能够修复,但无法确定。


2

您应该使用UIAlertController来替换iOS8中的所有AlertView和ActionSheet。

如果您的目标低于iOS8,则应检查版本并添加更多代码。

例如,以下代码适用于iOS8 ActionSheet:

-(void)testActionSheet{
    UIAlertController* alertAS = [UIAlertController alertControllerWithTitle:@"Test ActionSheet"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Action" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              NSLog(@"Action");
                                                          }];
    [alertAS addAction:defaultAction];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

    }];
    [alertAS addAction:cancleAction];
    [self presentViewController:alertAS animated:YES completion:nil];

}

2
如果您想继续使用现有的API,则有一些行为可以让您知道何时运行代码以及何时忽略委托调用 - buttonIndex
第一次调用委托方法时,它们总是传递正确的buttonIndex。然而,第二次调用时,它们会使用取消按钮的buttonIndex

1
我遇到了相同的问题。一个解决方法是:使用 actionSheet.tag。在实例化时将其设置为有效数字(默认为0),例如1、2等。在以下方法中处理响应:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

在处理响应之前,检查其是否有效(例如,在处理之前不为-1)。一旦在此处处理响应,在返回之前,请设置:

actionSheet.tag = -1;

这确保了即使进行第二次调用,你也会忽略它。在我的情况下,这很有效。



0
我想补充一点:UIAlertController 是在 iOS8.0+ 中使用 UIAlertViews 的正确方式(因为 UIAlertView 已被弃用),但是能够选择多个选项的 bug 有所缓解。
警报视图中的单独选项可以同时被选择/突出显示,但委托方法似乎只触发其中一个。哪一个实际上会触发是不确定的,但我可以确认,如果您使用多个手指,则只会触发一个,尽管两个或更多选项被突出显示。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接