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
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页