Swift 2.0 UIImagePickerController在关闭时解除父视图

时间:2015-10-01 02:14:39

标签: ios xcode uiimagepickercontroller swift2

开始在Xcode 6中开发应用程序。*使用2.0之前版本的swift。自从更新到Xcode 7.0和swift 2.0后,我的应用程序没有按预期运行。

在我的应用程序中,我有一个典型的用户设置视图,可以将其推送到我的NavigationController,如:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let view = storyBoard.instantiateViewControllerWithIdentifier("settingsView")
controller.navigationController?.setViewControllers([view], animated: false)

当用户点击其个人资料图片时,会显示UIImagePickerController。然而,现在,如果用户点击取消或选择图像,则图像控制器被解除,但用户设置控制器也是如此。为什么会突然发生这种情况(它在Xcode更新之前按预期工作)。

处理图片选择的代码如下:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        self.imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
        self.Picture.contentMode = .ScaleAspectFill
        self.Picture.image = pickedImage
        self.PictureChosen = true
    }

    dismissViewControllerAnimated(true, completion: nil)
}

3 个答案:

答案 0 :(得分:2)

当出现UIImagePickerViewController父控制器为UITabBarController时,我遇到了类似的问题,只需将modalPresentationStyle设置为overCurrentContext

let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
imagePicker.delegate = self
imagePicker.modalPresentationStyle = .overCurrentContext
present(imagePicker, animated: true, completion: nil)

答案 1 :(得分:0)

您是否将视图添加到当前的navigationController中?为什么不推送/显示settingsViewController? 如果你不推新控制器并调用dismissViewControllerAnimated,它将关闭附加视图的NavigationController。或者我想念一些东西。

嗯...尝试推送settingsController

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("settingsView")
controller.navigationController?.pushViewController(viewController, animated: true)

答案 2 :(得分:0)

不是很明白,但对我来说,你的:

dismissViewControllerAnimated(true, completion: nil)

关闭当前控制器,所以设置一个......没有?

你应该这样做:

pickerController.dismissViewControllerAnimated(true) { _ in
    //Do what you want when picker is dismissed
}

Morover,取消行动,更喜欢使用:

optional func imagePickerControllerDidCancel(_ picker: UIImagePickerController)

请参阅@ UImagePickerControllerDelegate Protocol Reference