swift - 如何以编程方式导航到 UIViewController?
问题描述
我在从另一个 UIViewController 以编程方式导航到 UIViewController 时遇到了困难。我正在使用 UIKit 和 Swift 5。
我试图关注这篇 Medium文章,但无法让它为我工作。
在我的 AppDelegate 文件中,我已将 didFinishLaunchingWithOptions 函数更改为:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = LoginViewController()
self.window?.makeKeyAndVisible()
return true
}
然后在我的 LoginViewController() 中,我尝试通过执行以下操作导航到我的 DashboardViewController:
override func viewDidLoad() {
super.viewDidLoad();
// present a modal with an embed UINavigationController
let rvc = DashboardViewController()
let vc = UINavigationController(rootViewController: rvc)
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true, completion: nil)
}
但是 DashboardViewController 永远不会被调用。
我做错了什么,如何更改代码才能工作?
谢谢你。
解决方案
我最终看到了一个帮助我解决问题的 Youtube 视频。
应用委托
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let login = LoginViewController()
let navigationController = UINavigationController(rootViewController: login)
navigationController.setNavigationBarHidden(true, animated: true)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
登录视图控制器
let dashboard = DashboardViewController()
self.navigationController?.pushViewController(dashboard, animated: true)
推荐阅读
- r - 如何总结R中单列中的所有变量
- html - CSS:Flex元素在包含输入时有额外的空间
- react-native - 为什么命令“run-android”无法识别?
- amazon-web-services - 使用动态分区通过 Lambda 创建 Firehose 数据流时出错
- javascript - 这可以在shopify中使用javascript获取默认商店国家名称吗?
- firebase - 如何让 Firebase auth OAuthProvider 使用 OIDC 提供程序添加其他范围
- python - 创建快捷方式返回错误,但在其他计算机上工作正常
- php - 检索附件 Sendgrid 入站解析
- node.js - Eelement 在正常(无 puppeteer 浏览器)中正确评估,但在 puppeteer 浏览器中不正确
- php - 如何在 PHP 中调用异步请求?因为我想返回数据而不必等待该请求的响应