从模态 ViewController 呈现 UIImagePickerController
Posted
技术标签:
【中文标题】从模态 ViewController 呈现 UIImagePickerController【英文标题】:Presenting UIImagePickerController from modal ViewController 【发布时间】:2017-03-07 09:27:04 【问题描述】:在我的故事板中有这样的部分:
所以我想从 UserProfileController 呈现:UIViewController "Мой профиль",它是模态的,(在图像顶部)UIImageViewController。
我的代码:
extension UserProfileController: UIImagePickerControllerDelegate, UINavigationControllerDelegate
@IBAction func imagePressed(_ sender: Any)
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
self.present(imagePickerController, animated: true, completion: nil)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else
return
picker.dismiss(animated: true, completion: nil)
userProfileImageView.image = image
好吧,imagePickerController 出现了,但是在我选择图像后,我已经关闭了两个控制器(imagePickerController 和父级 - UserProfileControler)。
我做错了什么?谢谢
附:可能问题是我将“SideMenuNavigationController”显示为模态? (在 SideMenu 框架文档中如此建议)
【问题讨论】:
可以尝试将@IBAction 放在扩展之外吗? 已经做了,IBAction同imagePicker:声明类体中的didFinish方法 【参考方案1】:尝试在你的类中创建 UIImagePickerController 引用(作为变量)
// to choose an image from the Camera Roll
fileprivate lazy var imagePicker: UIImagePickerController =
let picker: UIImagePickerController = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .savedPhotosAlbum
picker.delegate = self
return picker
()
【讨论】:
以上是关于从模态 ViewController 呈现 UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章
我应该在哪里创建和呈现整个 iPhone 应用程序中使用的模态 ViewController
无法从模态 ViewController 访问 UITabBarController
需要协助解除模态呈现的 ViewController 并从 UINavigationController 弹出 ViewController