Search code examples
swiftuinavigationcontrolleruitabbarcontroller

Mixed programmatic and storyboard. Button click will not open next VC in a navigation controller


I have a tab bar controller that is built programatically. And it looks something like this:

class NewTabBarController: UITabBarController {

override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self

        createTabbar()
    }

func createTabbar() {
let deliveryViewController = storyBoard.instantiateViewController(identifier: "DeliveryViewController") as? DeliveryViewController
        deliveryViewController?.tabBarItem.image = #imageLiteral(resourceName: "icon_deliver")
        deliveryViewController?.title = "Delivery"
        planDictionary["planType"] = Constants.Mealplan.deliveryPlan
        deliveryViewController?.planDictionary = planDictionary

// Excluded other tabs and view controller creations since they are the same
}

Now this DeliveryViewController is created in storyboard and embedded in a nav controller

It has a button click action:

@IBAction func saveNameButton(_ sender: UIButton) {



let addressViewController = storyBoard.instantiateViewController(identifier: "AddressViewController") as? AddressViewController
                addressViewController?.planDictionary = planDictionary
                navigationController?.pushViewController(addressViewController!, animated: true)
}

The button click action was working when the tabbar was in the storyboard. But after refactoring programatically, it is does not place the next VC on the navigation stack.

Help would be appreciated.


Solution

  • Almost certainly...

    This line:

    let deliveryViewController = storyBoard.instantiateViewController(identifier: "DeliveryViewController") as? DeliveryViewController
    

    Is instantiating an instance of DeliveryViewController and setting that as the View Controller for a Tab. But what you want to do is load a UINavigationController and make DeliveryViewController the root view controller of that NavController, and set that NavController as a Tab item.