Search code examples
iosswiftuinavigationcontroller

How to embed a navigation controller inside a custom framework


I have created a custom framework with storyboard. However eventhough the root viewcontroller been embedded froma navigation controller, every time i check "self.navigationController" it prints "nil". What am I missing here.

However my objective is to pop back to the root viewController once I click a button from my 4th VC. currently the implementation as follows

My Custom Storyboard looks like bellow.

enter image description here

How I navigate as bellow.

            if let urlString = Bundle.main.path(forResource: "FAUMESDK", ofType: "framework", inDirectory: "Frameworks") {
                let bundle = (Bundle(url: NSURL(fileURLWithPath: urlString) as URL))
                let sb = UIStoryboard(name: "FAUMEStoryboard", bundle: bundle)
                let vc = sb.instantiateViewController(withIdentifier: "MessagePriviewVC")
                vc.modalPresentationStyle = .fullScreen
                self.show(vc, sender: nil)
            }

I tried with the bellow code but didnt work

    let vc = self.storyboard?.instantiateViewController(
                withIdentifier: "MyVCIdentifier") as! MessagePreviewUIViewController

            self.navigationController?.pushViewController(vc, animated: true)

The way I'm trying to pop back to the rootVC as below (currently its not working, and this is where I need a solution).

navigationController?.popToRootViewController(animated: true)

What am I missing here ???


Solution

  • Although you have embedded your view controller in a navigation controller in your storyboard, you are requesting a specific view controller by its identifier; so you get just that view controller, not that view controller embedded in a UINavigationController.

    If you use instantiateInitialViewController then you will get the navigation controller that is the entry point to your storyboard, and it's root view controller will be your MessagePreviewViewController (Assuming that is the name of the view controller immediately after the navigation controller in your storyboard)

    if let urlString = Bundle.main.path(forResource: "FAUMESDK", ofType: "framework", inDirectory: "Frameworks") {
        let bundle = (Bundle(url: NSURL(fileURLWithPath: urlString) as URL))
        let sb = UIStoryboard(name: "FAUMEStoryboard", bundle: bundle)
        let vc = sb.instantiateInitialViewController()
        vc.modalPresentationStyle = .fullScreen
        self.show(vc, sender: nil)
    }