如果我在不使用导航控制器的情况下以编程方式呈现 ViewController,新的 VC "replace" 是旧的,还是堆叠在顶部?

If I present a ViewController programmatically without using a Navigation Controller, does the new VC "replace" the old one, or does it stack on top?

我是 iOS 的新手。

我有一个应用程序,通过该应用程序的路径可能会根据我从 API 获取的配置而有所不同。因此,我不使用 segues,因为我需要创建一个从每个 ViewController(VC)到每个其他 VC 的 segue。它造成了我不想要的混乱。所以我改为像这样从一个屏幕导航到另一个屏幕:

func navigate(to viewController: String) {
    let storyboard = UIStoryboard(name: K.mainStoryBoard, bundle: nil)
    let nextVC = storyboard.instantiateViewController(identifier: viewController)
    self.present(nextVC, animated: true, completion: nil)
}

我的问题是:如果我将我的 VCs 嵌入到 NavigationController 中,我知道它会创建一个堆栈。当我到达最后一个屏幕时,我会调用 func popToRootViewController(animated: Bool) -> [UIViewController]? 并从头开始。但是,我不使用 NavigationController。那么,这是否意味着当我呈现下一个 VC 时,它会替换前一个,或者它是否堆叠在前一个之上?我试图防止内存泄漏,所以我想确保在我的应用程序 运行s 内存不足并崩溃之前,我不会一直将 VCs 堆叠在一起。

提前致谢

编辑

因此,在我最后的 VC 中,我创建了一个放松转场。我这样称呼它:performSegue(withIdentifier: "unwindToMain", sender: self)

并且在我的第一个 VC(我的应用程序中的初始 VC)中,我这样写:

 @IBAction func unwind( _ seg: UIStoryboardSegue) {

 }

第一次使用该应用程序时一切正常。最后一个 VC 回到拳头 VC。现在的问题是,当我再次尝试 运行 通过应用程序时(从 VC 1 开始,然后转到下一个),我现在收到此错误:

MyApp[71199:4203602] [Presentation] Attempt to present <MyApp.DOBViewController: 0x1038760c0> on <MyApp.ThankYouViewController: 0x112560c30> (from <MyApp.ThankYouViewController: 0x112560c30>) whose view is not in the window hierarchy.

为了理解这一点,DOBViewController 将是我想从 MainVC 转到的第二个 VC。 ThankYouViewController 是我最后的 VC。看起来好像它没有完全从堆栈中删除。谁能告诉我这是怎么回事?

这是否意味着当我展示下一个 VC 时,它会替换前一个,还是叠加在前一个之上?

The new one stacks on top of the previous one.

When you present a view controller, like

self.present(nextVC, animated: true, completion: nil)

The one you called .present on (self in this case) becomes presentingViewController for the nextVC instance.

The one you presented (nextVC in this case) becomes presentedViewController for the self instance.

这是一个非常简单的基本示例...

控制器在 Storyboard 中设置,每个控制器都有一个按钮,连接到相应的 @IBAction

DOBViewController 的故事板 ID 设置为“dobVC”。

ThankYouViewController 的故事板 ID 设置为“tyVC”。

MainVC 嵌入导航控制器(在故事板中)并且导航控制器设置为 Initial View Controller:

class MainVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
    
    @IBAction func pushToDOB(_ sender: Any) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "dobVC") as? DOBViewController {
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}

class DOBViewController: UIViewController {
    
    @IBAction func pushToTY(_ sender: Any) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "tyVC") as? ThankYouViewController {
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}

class ThankYouViewController: UIViewController {
    
    @IBAction func popToRoot(_ sender: Any) {
        navigationController?.popToRootViewController(animated: true)
    }
    
}