ios – 解雇UIImagePickerController也会解散呈现视图控制器

前端之家收集整理的这篇文章主要介绍了ios – 解雇UIImagePickerController也会解散呈现视图控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个使用tabbar系统的项目. tabbar项之一是JobPostingViewController.我将它嵌入UINavigationController中.在这个视图控制器中有一个名为add new job的UIButton.我实现了pushviewcontroller去CreateJobPostViewController.在那里我需要添加UI ImagePickerController来选择图像.当我点击完成按钮或从库中选择一个图像时,它会关闭到JobPostingViewController.但它应该去CreateJobPostViewController.
任何人请帮助我.提前致谢.

你可以在这里看到问题:

JobPostingViewController中的代码

@IBAction func openCreateJob(sender: AnyObject) {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("CreateJobPostViewController") as! CreateJobPostViewController
    self.navigationController?.pushViewController(vc,animated: true)
}

CreateJobPostViewController中的代码

@IBAction func addImages(sender: AnyObject) {
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .PhotoLibrary
    presentViewController(imagePicker,animated: true,completion: nil)
}
func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    picker.dismissViewControllerAnimated(true,completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    picker.dismissViewControllerAnimated(true,completion: nil)
}

解决方法

将Picker添加为子视图

尝试将imagepicker作为子视图添加到您的CreateJobPostViewController中,然后将其从代理中的父项中删除

@IBAction func openCreateJob(sender: AnyObject) {

var picker: UIImagePickerController = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
self.addChildViewController(picker)
picker.didMoveToParentViewController(self)
self.view!.addSubview(picker.view!)
}

然后

func imagePickerControllerDidCancel(picker: UIImagePickerController) {

    picker.view!.removeFromSuperview()
    picker.removeFromParentViewController()
    }

用于呈现

通过currentcontext显示选择器,例如编辑取消选择,

在演示之前使用picker.modalPresentationStyle = .overCurrentContext //

presentViewController(picker,completion: nil)
原文链接:https://www.f2er.com/iOS/333689.html

猜你在找的iOS相关文章