Swift:关于 presentScene() 的安全实践

Swift: Safe practices regarding presentScene()

在我的程序中,viewController 之间的某些转换是通过以下代码以编程方式管理的:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)

我一直想知道这种做法的安全性。当 presentScene 被调用时,它会对 "wipe" 它下面的屏幕做任何事情,还是只是在现有视图之上堆叠更多视图?如果没有,它是如何工作的?这也是管理这种程序化转换的最佳方式(就性能而言)吗?

首先,你不需要每次都创建一个 UIStoaryboard 的对象,除非你有与 Main 不同的名称。修改您的代码:

let newViewController = self.storyBoard!.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController

现在开始回答您的问题!当您在另一个 UIViewControllerUINavigationController 上呈现任何 UIViewController 时,呈现的控制器没有堆栈。

简而言之,您正在向被呈现者 UIViewController 模态呈现 UIViewController。在没有任何关系规则的情况下,这可能发生在任何 UIViewController 之间。演示者应注意关闭它所呈现的 VC。

希望这能消除您的疑虑。