Ios 在xib视图文件之间导航

Ios 在xib视图文件之间导航,ios,swift,navigation,Ios,Swift,Navigation,我正在做一个项目,这个项目没有故事板,它只有一个视图,作为具有自己类的xib文件,问题是:如何在这些xib视图之间导航? 或者是否可以将它们嵌入导航控制器中 我尝试了这个代码,但什么都没发生 let NC = UINavigationController() @IBAction func showUserProfile(_ sender: Any) { print("show Profile") let vc = UserProfile(

我正在做一个项目,这个项目没有故事板,它只有一个视图,作为具有自己类的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
添加到
UIViewController
中,并在其中导航

根据您的评论,您可以将它们预定义到VC实例中,并使用您的首字母创建一个
UINavigationController
,然后只需从
UINavigationController

评论更新

正如我从主视图中的评论中了解到的,您已经定义了
UINavigationController
只需替换
NC.pushViewController(vc,动画:true)
使用self.navigationController.pushViewController(vc,动画: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)
    }

你不能你可以在VC中添加这些视图并导航那你如何在它们之间导航?我将其中一个设置为初始视图控制器,但我需要在这些xib视图之间导航请检查我的答案,我将更新一个解决方案关于此注释这是UIVewcontroller固有的第一个类:类BlockListViewController:UIViewController,这是第二个视图控制器如何将第一个嵌入导航控制器和从它转到第二个类,将这些类视为xib文件而不是故事板问题是(通过xib导航)答案是否定的这是一个解决办法我不知道你的代码和实际问题这是UIVewcontroller固有的第一个类:类BlockListViewController:UIViewController,这是第二个视图控制器如何将第一个嵌入导航控制器并从中转到第二个,这些类作为xib文件查看故事板–为什么不显示navigationController的第一个类?如果是caselet mainView=BlockListViewController(nibName:“BlockListViewController”,bundle:nil),则隐藏导航栏(rootViewController:mainView)