ipad和iPhone在使用UIAlertController的时候出现的错误

如果只是手机使用,按照正常的操作就可以了,但是今天在pad上试了一把,结果奔溃了,为啥呢,打印结果大概意思就是,弹出来的那个框需要挂靠在某个视图上,直接上代码吧
这是一段选择头像的提示框:
// 编辑头像
- (void)editHeader:(UIButton *)btn{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"设置头像" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self openImagePicker:UIImagePickerControllerSourceTypeCamera];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            [self openImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];

    if([DeviceInfo deviceIsPhone]){

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

    }else{

        UIPopoverPresentationController *popPresenter = [alert
                                                         popoverPresentationController];
        popPresenter.sourceView = btn; // 这就是挂靠的对象
        popPresenter.sourceRect = btn.bounds;
        [self presentViewController:alert animated:YES completion:nil];
    }
}
判断设备的类

.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DeviceInfo : NSObject

// 判断设备是否是手机
+ (BOOL)deviceIsPhone;

@end

.m

#import "DeviceInfo.h"

@implementation DeviceInfo

// 返回时否是手机
+ (BOOL)deviceIsPhone{

    BOOL _isIdiomPhone = YES;// 默认是手机
    UIDevice *currentDevice = [UIDevice currentDevice];

    // 项目里只用到了手机和pad所以就判断两项
    // 设备是手机
    if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone) {

        _isIdiomPhone = YES;
    }
    // 设备室pad
    else if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad){

        _isIdiomPhone = NO;
    }

    return _isIdiomPhone;
}
@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值