首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iPad中快速显示“iPad”

如何在iPad中快速显示“iPad”
EN

Stack Overflow用户
提问于 2022-10-13 13:31:55
回答 1查看 26关注 0票数 1

我需要上传图片和pdf文件,所以我使用的是HSAttachmentPicker框架,我可以打开选择器选项,可以在iPhone中上传图像和pdf,但是当我单击“上传”按钮时,它会在iPad中崩溃。

错误:

您必须通过警报控制器的popoverPresentationController提供此弹出器的位置信息。您必须提供sourceView和sourceRect或barButtonItem。如果在呈现警报控制器时不知道此信息,则可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供该信息。

但是HSAttachmentPicker是一个框架,所以我应该在哪里为iPad添加代码

代码:,这是我在上传图像类中编写的代码

代码语言:javascript
复制
    let picker = HSAttachmentPicker()

    @IBAction func uploadButtonTapped(_ sender: UIButton) {
    picker.delegate = self
    picker.showAttachmentMenu()
    }

extension PostEnquiryViewController: HSAttachmentPickerDelegate {
func attachmentPickerMenu(_ menu: HSAttachmentPicker, showErrorMessage errorMessage: String) {
}

func attachmentPickerMenuDismissed(_ menu: HSAttachmentPicker) {
}

func attachmentPickerMenu(_ menu: HSAttachmentPicker, show controller: UIViewController, completion: (() -> Void)? = nil) {
    self.present(controller, animated: true, completion: completion)
}

func attachmentPickerMenu(_ menu: HSAttachmentPicker, upload data: Data, filename: String, image: UIImage?) {

    if !data.isEmpty && (filename as NSString).pathExtension == "pdf" {
        self.attachmentFiles.append(.init(data: data, fileName: filename))
        self.attachmentImages.append(.init(image: UIImage(named: "pdf")!, imageName: filename))
        self.uploadFileImage.image = UIImage(named: "pdf")
    }
    if let img = image {
        DispatchQueue.main.async {
            self.uploadFileImage.image = img
        }
        self.attachmentImages.append(.init(image: img, imageName: filename))
    }
}
}

这里是showAttachmentMenu框架文件中的代码:在这里添加iPad的代码。请指点。

代码语言:javascript
复制
  - (void)showAttachmentMenu {
self.selfReference = self;
UIAlertController *picker = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSString *showPhotosPermissionSettingsMessage = [NSBundle.mainBundle objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] && showPhotosPermissionSettingsMessage != nil) {
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:[self translateString:@"Take Photo"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (@available(iOS 14.0, *)) {
            [self validatePhotosPermissionsWithAccessLevel:PHAccessLevelAddOnly completion:^{
                [self showImagePicker:UIImagePickerControllerSourceTypeCamera];
            }];
        } else {
            [self validatePhotosPermissions:^{
                [self showImagePicker:UIImagePickerControllerSourceTypeCamera];
            }];
        }
    }];
    [picker addAction:takePhotoAction];
}

if (showPhotosPermissionSettingsMessage != nil) {
    if (@available(iOS 14.0, *)) {
        // the app already has access to the Photo Library so we're safe to add `Use Last Photo` here
        if ([PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite] == PHAuthorizationStatusAuthorized) {
            UIAlertAction *useLastPhotoAction = [UIAlertAction actionWithTitle:[self translateString:@"Use Last Photo"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self useLastPhoto];
            }];
            [picker addAction:useLastPhotoAction];
        }
    } else {
        UIAlertAction *useLastPhotoAction = [UIAlertAction actionWithTitle:[self translateString:@"Use Last Photo"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self validatePhotosPermissions:^{
                [self useLastPhoto];
            }];
        }];
        [picker addAction:useLastPhotoAction];
    }
}

if (showPhotosPermissionSettingsMessage != nil) {
    UIAlertAction *chooseFromLibraryAction = [UIAlertAction actionWithTitle:[self translateString:@"Choose from Library"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (@available(iOS 14, *)) {
            // don't request access to users photo library since we don't need it with PHPicker
            [self showPhotoPicker];
        } else {
            [self validatePhotosPermissions:^{
                [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
            }];
        }
    }];
    [picker addAction:chooseFromLibraryAction];
}

UIAlertAction *importFileFromAction = [UIAlertAction actionWithTitle:[self translateString:@"Import File from"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self showDocumentPicker];
}];
[picker addAction:importFileFromAction];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:[self translateString:@"Cancel"] style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    [self dismissed];
}];

[picker addAction:cancelAction];

[self.delegate attachmentPickerMenu:self showController:picker completion:nil];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-14 13:10:31

阅读popover演示文稿:文档是个好主意。

如果您查看HSAttachmentPicker提供的示例中的视图控制器,您将看到示例应用程序是如何处理它的。是的,在目标-C.但在Swift中,它几乎是一样的:

代码语言:javascript
复制
func attachmentPickerMenu(_ menu: HSAttachmentPicker, show controller: UIViewController, completion: (() -> Void)? = nil) {

    // popover will be non-nil if we're on an iPad
    if let popover = controller.popoverPresentationController {
        
        // we want the popover arrow pointing to the button
        popover.sourceView = self.uploadButton
        
    }

    self.present(controller, animated: true, completion: completion)
}

下面是一个完整的、可运行的示例:

代码语言:javascript
复制
import UIKit

class ViewController: UIViewController {

    let uploadButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemYellow
        
        uploadButton.setTitle("Tap to Upload", for: [])
        uploadButton.setTitleColor(.white, for: .normal)
        uploadButton.setTitleColor(.lightGray, for: .highlighted)
        uploadButton.backgroundColor = .systemBlue
        uploadButton.layer.cornerRadius = 8.0
        
        uploadButton.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(uploadButton)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            // let's put the button near the bottom of the screen
            uploadButton.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -40.0),
            uploadButton.widthAnchor.constraint(equalToConstant: 200.0),
            uploadButton.heightAnchor.constraint(equalToConstant: 60.0),
            uploadButton.centerXAnchor.constraint(equalTo: g.centerXAnchor),
        ])
        
        uploadButton.addTarget(self, action: #selector(uploadButtonTapped(_:)), for: .touchUpInside)
    }

    @IBAction func uploadButtonTapped(_ sender: UIButton) {
        let picker = HSAttachmentPicker()
        picker.delegate = self
        picker.showAttachmentMenu()
    }
}

extension ViewController: HSAttachmentPickerDelegate {
    func attachmentPickerMenu(_ menu: HSAttachmentPicker, showErrorMessage errorMessage: String) {
        // Handle errors
    }
    
    func attachmentPickerMenuDismissed(_ menu: HSAttachmentPicker) {
        // Run some code when the picker is dismissed
    }
    
    func attachmentPickerMenu(_ menu: HSAttachmentPicker, show controller: UIViewController, completion: (() -> Void)? = nil) {

        // popover will be non-nil if we're on an iPad
        if let popover = controller.popoverPresentationController {
            
            // we want the popover arrow pointing to the button
            popover.sourceView = self.uploadButton
            
        }

        self.present(controller, animated: true, completion: completion)
    }
    
    func attachmentPickerMenu(_ menu: HSAttachmentPicker, upload data: Data, filename: String, image: UIImage?) {
        // Do something with the data of the selected attachment, i.e. upload it to a web service
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74056651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档