2014-09-20 91 views
10

在UIActionSheet的iOS8上的委托方法多次调用UIActionsheet委托方法在ios8中调用两次?

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex 

我在ipad 2已检查与IOS 8

回答

6

它的IOS 8的错误..

作为@rob所述UIActionSheet是在IOS 8(UIActionSheetDelegate也弃用。)弃用

要创建并在IOS 8管理动作片和后,使用UIAlertController

+2

呃,iOS 8.0.x已经成为iOS版本以来的最bu ... ...... iOS 4.0是什么? – Epaga 2014-10-13 07:15:42

4

这确实是我见过的报道在几个地方的当前行为。它似乎是一个错误,希望很快就会被修复,但无法确定。

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已被弃用),但是能够选择多个选项的错误有所缓解。

警报视图中的单独选项可以同时被选中/突出显示,但委托方法似乎只能触发其中一个选项。哪一个实际触发是不确定的,但是我可以确认,如果使用多个手指,尽管突出显示两个或更多个,但只有一个被触发。