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

2024-03-11

当我显示时出现运行时异常UIAlertController(操作表) in iOS8 Beta5+ Xcode6.

这个Bug只发生在iPad设备。

使用 UIAlertController 时出现以下异常。

*由于未捕获的异常“NSGenericException”而终止应用程序,原因:“UIPopoverPresentationController” (<_uialertcontrolleractionsheetregularpresentationcontroller:>) 应该设置一个非零的 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] ;

在 iPad 上,警报将使用新功能显示为弹出窗口UIPopoverPresentationController https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html,它要求您使用以下三个属性之一指定用于呈现弹出窗口的锚点:

  • 栏按钮项 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html#//apple_ref/occ/instp/UIPopoverPresentationController/barButtonItem
  • 源视图 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html#//apple_ref/occ/instp/UIPopoverPresentationController/sourceView
  • 源矩形 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html#//apple_ref/occ/instp/UIPopoverPresentationController/sourceRect

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

alertController.popoverPresentationController.sourceView = parentView;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示 UIAlertController(actionsheet) iOS8 时出现运行时异常 的相关文章

随机推荐