以编程方式搜索时检索ViewController的堆栈 [英] retrieving stack of ViewControllers when segueing programmatically

查看:67
本文介绍了以编程方式搜索时检索ViewController的堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static func showMenuView(parentVC:UIViewController){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let resultController = storyboard.instantiateViewController(withIdentifier: "menuViewController") as? MenuViewController;
    let segue:DragDownSettingSegue = DragDownSettingSegue.init(identifier: "", source: parentVC, destination: resultController);
    parentVC.prepare(for: segue, sender: nil);
    segue.perform();
}

我正在以编程方式选择视图控制器. ViewController A到B,然后B到C,然后C到D,然后D到B.B中有一个返回"按钮.所以我想从B返回时转到A. ,当D到B时,我想从堆栈而不是A和B中清除所有视图viewcontroller. 问题:我找不到堆叠的视图控制器的列表.

I am programmatically segueing the viewcontrollers. ViewController A to B, and then B to C, and then C to D, an then D to B. There is a button "Back" in B. So i want to go to A when i go back from B. so for this, when D to B then i want to clear all the view viewcontroller from stack instead of A and B. Issues: I can't get the list of stacked view-controllers.

推荐答案

使用展开搜索:)

您不需要手动清除viewController的堆栈.从B到A拖动一个放松的序列.这就是您所需要的:)

You don't need to clear the viewController's stack manually. Drag a unwind segue from B to A. Thats all u need :)

我相信您对UnwindSegue的概念感到困惑.放松搜索不仅是从一个视图控制器跳到另一个视图控制器的方法.当您执行从ViewController B到View Controller A的Unwind segue时,不仅将加载ViewController A,而且还将取消分配在其中插入的所有中间ViewController.

I believe you have confused the concept of UnwindSegue. Unwind segue is not just way to hop from one view controller to another. When you perform the Unwind segue from ViewController B to View Controller A, not just the ViewController A will be loaded but all the intermediary ViewControllers pushed in between will be de allocated as well.

这是证明:)

我的ViewController A的ViewwillAppear

My ViewController A's ViewwillAppear

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print(self.navigationController?.viewControllers.count ?? 0)
}

我的ViewController A放松心情

My ViewController A's unwind segue

@IBAction func unwindToA(segue: UIStoryboardSegue) {
    //perform whatever u have to do before unwind segue completes 
}

这是我的流程.

VCA -pushes-> VCB -pushes-> VCC

VCA -pushes-> VCB -pushes-> VCC

然后我从VCC到VCA进行隔离.因此,正如预期的那样,在执行windingSegue VCA的viewWillAppear将被调用.正如预期的那样,navigationController的viewController计数为1.这是我的VCA.因此很明显,VCB和VCC一起被释放了.

I then do unwind segue from VCC to VCA. So as expected on performing unwindSegue VCA's viewWillAppear gets called. And as expected the navigationController's viewController count is 1. Which is my VCA. So obviously VCB was deallocated along with VCC.

您可以编写dealloc/deinit来确认释放:)

You can write dealloc/deinit to confirm the deallocation :)

最后,当您可以使用诸如segueunwind segue以及popToRootViewController之类的适当构造实现相同的目标时,永远不要手动与NavigationController的ViewController堆栈混淆.

Finally never mess with NavigationController's ViewController stack manually when u can achieve the same with proper constructs like segue and unwind segue and popToRootViewController like methods.

希望有帮助

这篇关于以编程方式搜索时检索ViewController的堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆