在 xib 视图文件之间导航

navigate between xib view files

我正在做一个项目,该项目没有故事板,它只有一个 xib 文件的视图和它自己的 class,问题是:我如何在这些 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

A container view controller that defines a stack-based scheme for navigating hierarchical content.

A navigation controller is a container view controller that manages one or more child view controllers

所以基本上是一堆UIViewController,定义为[UIViewController]

documentation

中阅读更多相关信息

您可以做的是将每个 UIView 添加到 UIViewController 中,然后简单地导航。

根据您的评论,您可以将它们预定义到 VC 的实例中,并使用您的初始值创建一个 UINavigationController,然后只需从 [=13] 推送到所需的 UIViewController =]

评论更新

正如我从您在主视图中的评论中了解到的,您已经定义了 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)
    }