在xib视图文件之间导航

时间:2020-09-07 20:08:08

标签: ios swift navigation

我正在开发一个项目,该项目没有情节提要,它只有一个带有自己类的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)

1 个答案:

答案 0 :(得分:2)

您无法在UIView的实例之间导航

根据苹果UINavigationController

一个容器视图控制器,它定义了用于导航分层内容的基于堆栈的方案。

导航控制器是容器视图控制器,用于管理 一个或多个子视图控制器

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

documentation

中详细了解

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

根据您的评论,您可以将它们预定义为VC实例,并在初始时创建一个UINavigationController,然后只需从UIViewController推入所需的UINavigationController

评论更新

正如我从主视图中的评论中所获得的那样,您已经定义了UINavigationController,只需替换 NC.pushViewController(vc,animated: true )self.navigationController.pushViewController(vc, animated: true )

问题是您已经在嵌入第一个UINavigationController的同时创建了新的willConnectTo

评论更新: 如果您使用带有场景委托的iOS 13+,请在 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) }

像这样

{{1}}
相关问题