如何在不关闭整个堆栈的情况下关闭导航控制器中的视图控制器(How to dismiss a view controller in a navigation controller, without dismissing the whole stack)

基本上我有标签栏控制器有两个标签,每个标签都有一个单独的导航控制器,每个控制器都有几个视图(见下文)。

在“顶部”导航控制器(保存在右侧选项卡中),我可以选择一个图像,然后我被带到最右边的屏幕预览图像...

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let image = info[UIImagePickerControllerEditedImage] as? UIImage { let previewController = self.storyboard?.instantiateViewController(withIdentifier: "previewVC") as! PreviewViewController previewController.img = image previewController.navigationItem.setHidesBackButton(true, animated: false) self.navigationController?.pushViewController(previewController, animated: true) self.dismiss(animated: true, completion: nil) } }

...我点击“发布”。 这会上传图像并调用self.tabBarController?.selectedIndex = 0

这只需要我到图像Feed,它位于“底部”导航控制器(左侧标签)中。 到现在为止还挺好。 但是,当我需要在发布后显示上传屏幕时,预览屏幕仍显示在“右侧”选项卡中。

如果我点击发帖时调用self.dismiss(animated: true, completion: nil) ,整个导航控制器将被解除,我将被带回登录屏幕。

因此,我试图关闭预览控制器,以便上传控制器在右侧选项卡中显示为默认视图,而不会忽略该导航控制器中的整个堆栈。

我怎样才能做到这一点?

Basically I have tab bar controller with two tabs, each holds a separate navigation controller, each with a couple of views (see below).

In the "top" navigation controller (held in the right tab), I can choose an image, and then am taken to the rightmost screen to preview the image...

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let image = info[UIImagePickerControllerEditedImage] as? UIImage { let previewController = self.storyboard?.instantiateViewController(withIdentifier: "previewVC") as! PreviewViewController previewController.img = image previewController.navigationItem.setHidesBackButton(true, animated: false) self.navigationController?.pushViewController(previewController, animated: true) self.dismiss(animated: true, completion: nil) } }

...where I tap "post". This uploads the image and calls self.tabBarController?.selectedIndex = 0

That simply takes me to the image feed, which is in the "bottom" navigation controller (left tab). So far so good. However, the preview screen is still displayed in the "right" tab, when I need the upload screen to be displayed after I post.

If I call self.dismiss(animated: true, completion: nil) when I tap post, the entire navigation controller is dismissed and I'm taken back to the login screen.

So I'm trying to dismiss the preview controller so the upload controller is shown as the default view in the right tab, without dismissing the entire stack in that navigation controller.

How can I do this?

最满意答案

如果使用导航控制器将视图控制器添加到导航堆栈(推送),则可以通过调用pop函数将其删除。 以模态方式显示视图控制器时,调用dismiss删除视图控制器。

If you use navigation controllers to add view controllers to the navigation stack (push), you can remove them by calling a pop function. When you present view controllers modally, you call dismiss to remove the view controller.

更多推荐