[英]Push Navigation Controller into Navigation Controller
我有一個 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()
}
視圖控制器中的函數
@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”而終止應用程序,原因:“不支持推送導航控制器”*首先拋出調用堆棧:
看起來您正在從故事板實例化導航控制器。 獲取頂視圖控制器。
@objc func buttonTapped(){
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
if let top = viewController.topViewController {
navigationController?.pushViewController(top, animated: true)
}
print("button tapped")
}
這里有許多項目似乎是錯誤的。 您沒有將ViewController
設置為NavigationController
rootViewController
。 您正在嘗試推送一個新的 ViewController,它是另一個 NavigationController。 這是你需要的。
第一個:UISceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = UINavigationController(rootViewController: FirstViewController())
window?.makeKeyAndVisible()
}
第二個:ViewController.swift
@objc func navigate() {
tabBarController?.navigationController?.pushViewController(SecondViewController(), animated: true)
}
注意:SecondViewController 是 UIViewController 類型而不是 UINaviationViewController。
在函數導航中,首先使用 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.