以编程方式进行segueing时检索ViewControllers的堆栈 [英] retrieving stack of ViewControllers when segueing programatically

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

问题描述

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中有一个Back按钮。所以当我从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.

推荐答案

使用unwind segue:)

Use unwind segue :)

您无需手动清除viewController的堆栈。将展开的segue从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的概念。展开segue不仅仅是从一个视图控制器跳到另一个视图控制器的方式。当您从ViewController B执行Unwind segue到View Controller A时,不仅会加载ViewController A,而且所有中间推送的ViewControllers也将被分配。

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的展开segue

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解开segue。正如预期的那样,在执行unwindSegue时,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 :)

最后,当你可以通过适当的结构(如 segue 展开segue )实现相同的操作时,不要手动使用NavigationController的ViewController堆栈。 popToRootViewController 类似方法。

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.

希望有所帮助

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

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