简体   繁体   English

在 segue 后关闭视图控制器

[英]Close a viewcontroller after a segue

What I want is to close a viewController after performing a segue so that the back button of the navigation controller on the new view doesn't go back to the view that I just closed, but it goes to the view that precedes it in the storyboard like it is the first time that it is loaded.我想要的是在执行 segue 后关闭viewController ,以便新视图上导航 controller 的后退按钮不会 go 回到我刚刚关闭的视图,但它会转到 ZCD495490C007200 中它之前的视图就像它是第一次加载一样。

在此处输入图像描述

I already tried stuff like dismiss and so but it doesn't really work for me as it only closes the view in which the button that I pressed for performing the function is located:我已经尝试过诸如dismiss之类的东西,但它对我来说并没有真正起作用,因为它只会关闭我为执行 function 按下的按钮所在的视图:

@objc func goToList(){
   self.dismiss(animated: true, completion: nil)
   performSegue(withIdentifier: "goToList", sender: nil)
}

You can use unwind segue to get back to each viewController that you want.您可以使用unwind segue回到您想要的每个viewController
Read more here:在这里阅读更多:
Unwind Segues Step-by-Step (and 4 Reasons to Use Them)逐步展开 Segues(以及使用它们的 4 个理由)

The navigation controller maintains a stack (array) of ViewControllers that have been opened (pushed).导航 controller 维护已打开(推送)的 ViewController 的堆栈(数组)。 It also has the ability to pop these ViewControllers off the stack until it gets to a specific one.它还能够将这些 ViewController 从堆栈中弹出,直到它到达特定的一个。

For example, if you wished to return to a previous view controller of type MyInitialVC then you'd want to search through the stack until you found that type of VC, and then pop to it:例如,如果您希望返回到 MyInitialVC 类型的上一个视图MyInitialVC ,那么您需要在堆栈中搜索,直到找到该类型的 VC,然后弹出到它:

let targetVC = navigationController?.viewControllers.first(where: {$0 is MyInitialVC})
if let targetVC = targetVC {
   navigationController?.popToViewController(targetVC, animated: true)
}

NB.注意。 written from memory without XCode, so you may need to correct minor typos写自 memory 没有 XCode,所以你可能需要纠正轻微的错别字

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

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