【问题标题】:Dismissing of UIImagePickerController dismisses presenting view controller also解除 UIImagePickerController 也会解除当前的视图控制器
【发布时间】:2016-11-05 03:24:06
【问题描述】:

我正在做一个使用标签栏系统的项目。 tabbar 的一项是 JobPostingViewController。我将它嵌入到 UINavigationController 中。在这个视图控制器中有一个名为添加新作业的 UIButton。我实现了 pushviewcontroller 去 CreateJobPostViewController。我需要添加 UIImagePickerController 来选择图像。当我点击完成按钮或从库中选择图像时,它会关闭 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)
}

【问题讨论】:

  • 我以前从未见过这个问题。 imagePicker 似乎在调用 addImages 之前已经定义。为什么?那是在其他地方使用吗?是否有其他代码同时工作?另外,您正在调用picker.dismissViewControllerAnimated,您应该要求父级(presentingViewController)解除模态控制器(即self.dismissViewControllerAnimated),而不是告诉被呈现者自行解除。
  • 将 imagePicker 定义为全局变量没有其他意义。它不在任何地方使用。我尝试了您建议关闭图像选择器视图控制器的两个选项。我厌倦了寻找解决方案,我使用了另一种方法。谢谢您的建议。 @西蒙麦克劳林
  • 能否多发些代码或多解释一下应用的布局。有很多人强调各种类似的问题和解决方案。 JobPostingViewController 是容器视图吗?还是在容器视图内?这个屏幕是怎么打开的? when the picker is dismissed it will tr​​igger the viewWillAppear / viewDidAppear, viewDidLayoutSubviews methods on CreateJobPostViewController.你在这些方法中做了什么吗?
  • 当拾取器被拒收时,也不调用这些方法。它直接加载标签栏项目。我已经创建了一个在名为 TabViewController 的类中创建标签栏的方法。它直接调用该方法并重新加载标签栏。
  • 非常感谢西蒙。毕竟我解决了这个问题。实际上,以编程方式创建标签栏存在问题。因为你,我解决了问题。非常感谢。我很感激。 :) :) :D

标签: ios objective-c iphone swift uiimagepickercontroller


【解决方案1】:

通过将图像选择器的 modalPresentationStyle 设置为“OverCurrentContext”解决了这个问题:

picker.modalPresentationStyle = .overCurrentContext

【讨论】:

  • 这实际上为我修复了它。 viewWillDisappear 方法不再在原始视图控制器上调用。
  • 这是正确的做法。接受的答案是一个哈克。
【解决方案2】:

将 Picker 添加为子视图

尝试将图像选择器作为子视图添加到您的 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()
    }

用于展示

在当前上下文中显示选择器,并带有编辑取消选择等选项,

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

 presentViewController(picker, animated: true, completion: nil)

【讨论】:

  • 我想在导航栏和标签栏上方显示选择器,这意味着在整个屏幕上。你能帮帮我吗?
  • 您可以通过将您的选择器控制器添加为 self.navigationcontroller 的子视图来做到这一点,例如 [self.navigationController.view addSubview:picker] by
【解决方案3】:

你可以使用:

picker.modalPresentationStyle = .overCurrentContext

但根据您的屏幕布局,使用它可能会更好:

picker.modalPresentationStyle = .overFullScreen

否则您的“取消”、“重拍”和“使用照片”按钮可能不可见。

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,我使用情节提要解决了它(Xcode v9.2)

    1 - 转到情节提要,您的 ViewController 在哪里,选择它

    2 - 将演示设置为:当前上下文

    注意:如果当前上下文不起作用,请选择 Over Current Context

    【讨论】:

      【解决方案5】:

      我试图在 iOS 13 弹出视图控制器上显示选择器。为了阻止相机关闭我的弹出视图控制器,我必须将选择器的 modalPresentationStyle 更改为弹出框。见下文:

      picker.modalPresentationStyle = .popover
      

      【讨论】:

        猜你喜欢
        • 2017-02-27
        • 2017-01-27
        • 2014-04-27
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-12
        相关资源
        最近更新 更多