Your application has presented a UIAlertController (<UIAlertController: 0x100b79

本文介绍了在iOS开发中使用UIAlertController呈现ActionSheet样式弹框时遇到的问题:当应用支持iPad时,若未正确配置popoverPresentationController,则会导致崩溃。文章提供了具体的代码示例及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错信息:

Your application has presented a UIAlertController (<UIAlertController: 0x100b790f0>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.
复制代码

ActionSheet样式弹框:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"act1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"act2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action];
    [alert addAction:action2];
    [alert addAction:cancle];
    [self presentViewController:alert animated:YES completion:nil];
复制代码

设置仅在iPhone: 路径: TARGETS -> Development Info -> Devices

上面创建UIAlertControllerStyleActionSheet样式弹框的代码在iPhone手机上运行时没有问题,运行效果如图(四周存在黑边是因为Xcode设置仅在iPhone运行 而模拟器放在了iPad上不适配所致): 此时可看到UIAlertControllerStyleActionSheet运行效果iPhone与iPad效果一致.

设置Universal:

设置机型为Universal(即: 手机和iPad均支持) 在iPhone上运行效果与上一致,而在iPad上运行时 导致崩溃

解决:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
   UIAlertAction *action = [UIAlertAction actionWithTitle:@"act1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
   }];
   UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"act2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
   }];
   UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
   }];
   [alert addAction:action];
   [alert addAction:action2];
   [alert addAction:cancle];
   UIPopoverPresentationController *popover = alert.popoverPresentationController;
   if (popover)
   {
       popover.sourceView = self.view;
       popover.sourceRect = CGRectMake(0, 0, 100, 100);
       popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
   }
   [self presentViewController:alert animated:YES completion:nil];
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值