显示UIAlertController(actionsheet)iOS8时出现的运行时异常

时间:2014-09-03 08:40:36

标签: ios8 uialertcontroller

当我在 iOS8 Beta5 + Xcode6 中显示 UIAlertController(ActionSheet)时,

运行时异常即将到来。

此错误仅发生在 iPad 设备中。

我在使用UIAlertController时遇到了轰鸣声异常。

  

* 由于未捕获的异常终止应用程序' NSGenericException',原因:' UIPopoverPresentationController   (小于_UIAlertControllerActionSheetRegularPresentationController:   0x15794370>)应该设置非零的sourceView或barButtonItem   在演示发生之前。'

     

我的代码显示ActionSheet如下

     // Cancel Button
      UIAlertAction *actionCancel = [UIAlertAction
                                               actionWithTitle:NSLocalizedString(@"IDS_LABEL_CANCEL", nil)
                                               style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                                                   // cancel
                                                   //action handler
                                                   [self actionHandler:nil withTag:0 withButtonIndex:0];
                                               }];

      // print button
      UIAlertAction *actionPrint = [UIAlertAction
                                                      actionWithTitle:NSLocalizedString(@"IDS_LABEL_PRINT", nil)
                                                      style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

                                                          //action handler
                                                          [self actionHandler:nil withTag:kAttachmentActionSheetTag withButtonIndex:0];         
                                             }];

    // Create action sheet
     UIAlertController *alertController = [UIAlertController
                                                      alertControllerWithTitle:nil message:nil
                                                      preferredStyle:UIAlertControllerStyleActionSheet];

[alertController addAction:actionCancel];
[alertController addAction:actionPrint];

     // show aciton sheet
     [self  presentViewController:alertController animated:YES
                                 completion:nil] ;

1 个答案:

答案 0 :(得分:5)

在iPad上,警报将使用新的UIPopoverPresentationController显示为弹出窗口,它要求您使用以下三个属性之一指定弹出窗口的定位点:

为了指定锚点,您需要获取对UIAlertController的UIPopoverPresentationController的引用,并设置其中一个属性,如下所示:

alertController.popoverPresentationController.sourceView = parentView;