iOS 开发:如何将 UINavigationController 推送到当前视图控制器
在 iOS 开发中,可以通过以下步骤将一个 'UINavigationController' 推送到当前的 'UIViewController' 中:
- 首先,确保当前的 'UIViewController' 已经嵌入在一个 'UINavigationController' 中。如果没有,可以使用以下代码将当前的 'UIViewController' 包裹在一个 'UINavigationController' 中:
let navigationController = UINavigationController(rootViewController: currentViewController)
- 然后,创建一个新的 'UIViewController',即将被推送的视图控制器:
let newViewController = NewViewController()
- 使用 'UINavigationController' 的 'pushViewController(_:animated:)' 方法将新的视图控制器推送到导航控制器中:
navigationController?.pushViewController(newViewController, animated: true)
以上代码将在导航控制器的堆栈中添加一个新的视图控制器,并且使用动画将其推送到屏幕上。
请注意,以上代码中的 'currentViewController' 是当前的视图控制器,可以根据具体情况进行替换。另外,如果你在一个 'UITabBarController' 中使用导航控制器,你需要将导航控制器设置为 'UITabBarController' 的一个子视图控制器。
原文地址: https://www.cveoy.top/t/topic/o4gN 著作权归作者所有。请勿转载和采集!