navigate between xib view files

Viewed 674

I'm working on a project, the project doesn't have storyboards, it just have a views as xib files with its own class , the question is : how can I navigate between these xib views ? or is it possible to embed them in navigation controller ?

I try this code but nothing happened ?

let NC = UINavigationController()

 @IBAction func showUserProfile(_ sender: Any) {
    
    print("show Profile")
    let vc = UserProfile(
    nibName: "UserProfile",
    bundle: nil)
    NC.pushViewController(vc,
    animated: true )
}

this is in app delegate

 let mainView = BlockListViewController(nibName: "BlockListViewController", bundle: nil)
    window?.addSubview(mainView)
    let navigationControll = UINavigationController(rootViewController: mainView)
    self.window?.rootViewController = navigationControll
    self.window?.makeKeyAndVisible()

and I try to navigate when event occur using this

    self.navigationController?.pushViewController(UserProfile(), animated: true)
1 Answers

You can't navigate between instances of UIView

according to apple 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

so basically a stack of UIViewController, defined as [UIViewController]

read more about it in the documentation

What you can do is adding each UIView in a UIViewController and navigate thru that simply.

According to your comment you can predefined them into instance of VC and create a UINavigationController with you'r initial then simply push to the desired UIViewController from the UINavigationController

COMMENT UPDATE

As I got from your comment in the main view you already defining the UINavigationController simply replace NC.pushViewController(vc,animated: true ) with self.navigationController.pushViewController(vc, animated: true )

The problem is you are creating new UINavigationController while you already have the first one embedded

Comment update: if you're using iOS 13+ with scene delegate use this inside 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()

and call self.navigationController.push

like this

@IBAction func didTap(_ sender: UIButton)  {
        let secondView = secondVC(nibName: "secondVC", bundle: nil)
        self.navigationController?.pushViewController(secondView, animated: true)
    }
Related