关闭当前模态视图控制器,然后呈现新的模态视图控制器

Dismiss current modal view controller and then present new modal view controller

我正在尝试关闭一个模态控制器,然后通过按下第一个模态控制器上的按钮来显示一个新的模型控制器。我试过这个:

dismiss(animated: true, completion: {_ in
                self.navigateToAtmDetail(atmId:id)

            })

但是一旦当前视图控制器被关闭,它就会保持关闭状态并且不会导航到下一个控制器。此外,我们使用的是 nibs 而不是故事板和 segues,所以我无法访问它们。知道如何关闭当前的模态视图控制器而不是呈现新的模态视图控制器吗?

尝试:

let presenting = (self.presentingViewController.childViewControllers[0] as! <VIEWCONTROLLERTYPE>) //Or whatever index your controller is!
dismiss(animated: true, completion: {_ in
                presenting.navigateToAtmDetail(atmId:id)

            })

您的呈现控制器的类型在哪里。您还必须将 navigateToAtmDetail 方法移动到另一个控制器

简短的解释(更好,在下面的答案中有详细的解释)是你不能从一个已经被解雇的控制器上展示新的控制器。所以我们想回到呈现给我们的控制器 (presentingViewController),它恰好是 navigationViewController。这没有我们想要的逻辑,所以我们需要有 (childViewControllers[x]) 的子 viewcontroller。然后我们命令该控制器执行下一个表示逻辑。祝你好运!

我会尽量在这里给出一个更透彻的答案。在 iOS 中编程时,所有实例都必须从...其他实例创建。当我说实例时,我指的是已实例化的对象(已在计算机内存中创建的对象)。所以,每当你用XCode做项目的时候,你总是需要标记'the initial view controller'This is going to be the first instance that you create. Then it is used to spawn other instances. The reason why Apple chose this architecture is for security reasons (I think...? someone correct me if they have a better answer). You can see a very clear view of all the 'instances' of the views. You click this button while you are running a program in XCode . Then you can see the hierarchy of the views. I have made a simple program where clicking a button will load a different view. Where here I have not clicked the button and I have only loaded one view. However here I have clicked the button and loaded the next view, . At the top they both say UIWindow. That is because... (I hope you can guess this part!) They are both being instantiated from the UIWindow View. Don't believe me? Check this out! Here are the actual view hierarchies. and 。所以,如果你不明白我的意思。您应该明白,只需阅读您的问题,就很明显您正试图从另一个您试图解雇的模型控制器实例化您的模型控制器。因此,如果模型控制器已被解雇,应该如何实例化另一个模型控制器?还要记住,最好只根据用途命名控制器,例如 MenuViewController、MainController 或 VideoController。模型这个词通常用在 MVC 中,它不应该用作 ViewControllers 名称的一部分。 (希望我听起来没那么粗鲁哈哈,我曾经教过初中,所以这就是我教 XD 的方式)