ios - 在 xib 视图文件之间导航
问题描述
我正在做一个项目,该项目没有情节提要,它只有一个带有自己类的 xib 文件的视图,问题是:如何在这些 xib 视图之间导航?还是可以将它们嵌入导航控制器中?
我尝试了这段代码,但什么也没发生?
let NC = UINavigationController()
@IBAction func showUserProfile(_ sender: Any) {
print("show Profile")
let vc = UserProfile(
nibName: "UserProfile",
bundle: nil)
NC.pushViewController(vc,
animated: true )
}
这是在应用程序委托中
let mainView = BlockListViewController(nibName: "BlockListViewController", bundle: nil)
window?.addSubview(mainView)
let navigationControll = UINavigationController(rootViewController: mainView)
self.window?.rootViewController = navigationControll
self.window?.makeKeyAndVisible()
我尝试在事件发生时使用它进行导航
self.navigationController?.pushViewController(UserProfile(), animated: true)
解决方案
您无法在以下实例之间导航UIView
根据苹果UINavigationController
一个容器视图控制器,它定义了用于导航分层内容的基于堆栈的方案。
导航控制器是管理一个或多个子视图控制器的容器视图控制器
所以基本上是一堆UIViewController
,定义为[UIViewController]
在文档中阅读有关它的更多信息
您可以做的是将每个添加UIView
到 a 中UIViewController
并简单地导航。
根据您的评论,您可以将它们预定义到 VC 实例中并使用您的UINavigationController
初始值创建一个,然后只需UIViewController
从UINavigationController
评论更新
正如我从您在主视图中的评论中得到的那样,您已经定义了UINavigationController
简单的替换 NC.pushViewController(vc,animated: true )
为 self.navigationController.pushViewController(vc, animated: true )
问题是您正在创建新的UINavigationController
,而您已经嵌入了第一个
评论更新:如果您使用带有场景委托的 iOS 13+,请在内部使用willConnectTo
guard let scene = (scene as? UIWindowScene) else { return }
// Instantiate UIWindow with scene
let window = UIWindow(windowScene: scene)
// Assign window to SceneDelegate window property
self.window = window
// Set initial view controller from Main storyboard as root view controller of UIWindow
let mainView = BlockListViewController(nibName: "BlockListViewController", bundle: nil)
let navigationControll = UINavigationController(rootViewController: mainView)
self.window?.rootViewController = navigationControll
// Present window to screen
self.window?.makeKeyAndVisible()
并打电话self.navigationController.push
像这样
@IBAction func didTap(_ sender: UIButton) {
let secondView = secondVC(nibName: "secondVC", bundle: nil)
self.navigationController?.pushViewController(secondView, animated: true)
}
推荐阅读
- javascript - 刷新页面时 JavaScript 停止工作
- uno-platform - Uno.Material 是否应该安装在预发布的 Uno 中?
- jdbc - Athena最新的JDBC驱动jar AthenaJDBC42_2.0.14
- c - 为什么子线程中不能释放父进程的malloced内存空间?
- java - 如何使用类路径中的文件?
- jetbrains-ide - JetBrains Rider 停止在导航上打开差异标签
- c# - 如何在 Unity 中为脚本化变换动画添加缓动?
- elasticsearch - 弹性搜索子字符串搜索
- r - 在 R 中编写函数:将统计信息的名称(作为变量)粘贴为列名
- node.js - Angular 发生未处理的异常:EILSEQ:非法字节序列,已读取。当我运行“ng”命令时