Ipad弹出UIAlertControllerStyleActionSheet时发生崩溃

本文介绍了一个在iOS开发中遇到的问题:当使用UIAlertControllerStyleActionSheet样式时,在iPhone上正常显示,但在iPad上会崩溃。文章提供了解决方案,通过判断设备类型并在iPad上设置UIPopoverPresentationController来正确显示ActionSheet。

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

pad弹出UIAlertControllerStyleActionSheet时,在iphone上运行正常,但在ipad上崩溃,解决代码如下:

UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"请选择图片" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

 

    if (isiPad) {//判断是否为ipad

        UIPopoverPresentationController *popover = alertVc.popoverPresentationController;

        if (popover) {

            popover.sourceView = view;//视图中的某个view

            popover.sourceRect = view.bounds;

            popover.permittedArrowDirections = UIPopoverArrowDirectionDown;//alertVC弹出的位置

        }

    }

    

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        

    }];

    

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    

    }];

    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        [alertVc dismissViewControllerAnimated:YES completion:nil];

    }];

    

    [alertVc addAction:action1];

    [alertVc addAction:action2];

    [alertVc addAction:action3];

    

    [self presentViewController:alertVc animated:YES completion:nil];

 

转载于:https://www.cnblogs.com/lmg4819/p/7595783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值