我的行动表有点问题。在iPhone上运行良好,但在iPad上却崩溃了。
我有一个显示相机的按钮,但在iPad上无法正确显示。
代码:

class pictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate {
    @IBOutlet weak var picture: UIImageView!
    override func viewDidLoad() {
    }

    @IBAction func choosePictureAction(sender: AnyObject) {
        let alertPictureFrom = UIAlertController(title: "De dónde sacar la foto?", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
        presentViewController(alertPictureFrom, animated: true, completion: nil)
        alertPictureFrom.addAction(UIAlertAction(title: "Cámara", style: .Default, handler: {action in
            let picker = UIImagePickerController()
            picker.sourceType = .Camera
            picker.delegate = self
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        }))

        alertPictureFrom.addAction(UIAlertAction(title: "Galería", style: .Default, handler: {action in
            let picker = UIImagePickerController()
            picker.sourceType = .PhotoLibrary
            picker.delegate = self
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        }))
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        picture.image = image
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

}

您的应用程序提供了一个UIAlertControllerStyleActionSheet样式的UIAlertController()。具有此样式的UIAlertController的modalPresentationStyle为UIModalPresentationPopover。必须通过警报控制器的popoverPresentationController提供此弹出窗口的位置信息。必须提供sourceView和sourceRect或barbuttonem。如果在显示警报控制器时不知道此信息,可以在uipopopOverPresentationControllerDelegate方法-prepareForpoverPresentation中提供它。

最佳答案

如错误消息所示:“您必须通过popoverPresentationController提供sourceView和sourceRect或barbuttonem”
这将允许UIKit知道从何处呈现popover,然后您将获得popover附加到指定元素的可爱行为。
例如:

alertPictureFrom.popoverPresentationController?.sourceRect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
alertPictureFrom.popoverPresentationController?.sourceView = self.view

现在这个示例完全用于演示目的,您需要计算出popover的sourceViewsourceRect
参考文件:
popoverPresentationController
sourceRect
sourceView

关于ios - iPad上的ActionSheet崩溃(相机按钮),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34290429/

10-10 11:52