简体   繁体   English

如何在不删除整个堆栈的情况下关闭导航控制器中的视图控制器

[英]How to dismiss a view controller in a navigation controller, without dismissing the whole stack

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 这将上传图像并调用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. 如果我点击post时调用self.dismiss(animated: true, completion: nil) ,则整个导航控制器将被关闭,并返回到登录屏幕。

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? 我怎样才能做到这一点?

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. 模态显示视图控制器时,调用dismiss删除视图控制器。

The delegate function gives you the (image picker) view controller and you should dismiss that, not the view controller that is the delegate (usually the presenting view controller). 委托函数为您提供了(图像选择器)视图控制器,您应该关闭该控件,而不是作为委托的视图控制器(通常是呈现视图控制器)。 Maybe try something like this: 也许尝试这样的事情:

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)

        picker.dismiss(animated: true, completion: nil)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM