在 iOS 中切换 View Controller 的最佳方法有以下几种:
setViewControllers:
方法:let viewControllerArray = [firstViewController, secondViewController]
viewControllerArray.forEach { viewController in
viewController.willMove(toParent: nil)
}
viewControllerArray.forEach { viewController in
viewController.view.frame = view.bounds
}
view.setViewControllers(viewControllerArray, direction: .forward, animated: true, completion: nil)
popToViewController
方法:let navigationController = UINavigationController(rootViewController: secondViewController)
view.present(navigationController, animated: true, completion: nil)
presentViewController
方法:let thirdViewController = UIViewController()
thirdViewController.view.backgroundColor = .red
view.present(thirdViewController, animated: true, completion: nil)
以上三种方法都可以实现在 iOS 中切换 View Controller,具体使用哪种方法取决于你的需求和开发场景。
领取专属 10元无门槛券
手把手带您无忧上云