ios - 将导航控制器推入导航控制器
问题描述
我有一个 ViewController,它由以编程方式构建的屏幕组成。让我们说 FirstVc。在按钮上单击我想要的窗口,我的故事板 中的导航控制器(如图所示)
如图所示,我将 SecondViewController 嵌入到导航控制器中
在 OnButtonClick 函数中使用我当前的代码。它抛出错误无法推送导航控制器内部导航控制器。这是真的。但如果我只从 Main.storyboard 实例化 ViewController,我将无法看到顶部导航栏。
另外,我不想使用 .present ,因为我希望整个窗口发生变化。代表故事板的方括号。
FirstVc <---> [Navigatiob->Tab->SecondVC]
实现虚线表示的链接有哪些可能的方法?
场景代表
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
//
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let navigation = AppNavigationView()
let viewController = ViewController()
navigation.pushViewController(viewController, animated: true)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
}
ViewController 中的功能
@objc func buttonTapped(){
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
navigationController?.pushViewController(viewController, animated: true)
self.present(viewController, animated:true, completion:nil)
print("button tapped")
}
错误日志
-- 移动到活动状态 --- 2020-04-02 20:49:43.978018+0530 Snug[13340:370457] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持推送导航控制器”*首先抛出调用堆栈:
解决方案
在函数导航中,首先使用 let 常量设置第二个控制器,然后在 pushViewController 中调用它:
@objc func navigate() {
let secondController = SecondViewController()
navigationController?.pushViewController(secondController, animated: true)
}
像这样自然设置场景委托
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: ViewController())
window?.rootViewController = vC
}
推荐阅读
- go - Goroutines 在通道上监听相同的结果
- python - 如何评估重叠区间内的列表?
- python - Gensim word2vec 模型输出 1000 维 ndarray 但 ndarray 维的最大数量为 32 - 如何?
- python - 如何在 TensorFlow 2 中获取 Keras 张量的值?
- flutter - 从文本字段中检索值
- java - 有什么方法可以在 TextInputLayout 中使用(默认)SearchView?
- java - 带参数的Java getter?
- javascript - 在while循环外显示帖子描述
- python - 如何在 Python 3 中使用 smtplib.sendmail() 对收件人姓名(而非地址)中的国际字符进行编码?
- python - 使用 Python 将 csv 文件中的剥离文本保存为字符串对象