cosmos cosmos - 2 months ago 11
Swift Question

Navigation Bar Not Showing on Embedded TableViewController - Swift

I have a NavigationController embedded in my TableViewController. When I run the app everything works fine, but the Navigation Bar does not show.

This is how my code looks:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let box = boxes[indexPath.row]
print(box.name)



if let storyboard = self.storyboard {
let viewController = storyboard.instantiateViewController(withIdentifier: "BoxDescriptionTVC") as! BoxDescriptionTableViewController
self.present(viewController, animated: false, completion: nil)
viewController.boxDetailsLabel.text = box.boxDescription
if let boxImageURL = box.boxImageURL {
viewController.boxImageView.loadImageUsingCacheWithUrlString(urlString: boxImageURL)
}
}
}


How do I create an instance of my NavigationController to present it instead of the TableViewController?

Answer

First, you can set storyboardID for NavigationController in your storyboard. Then, you can write code like as follows. (I assume that your viewcontrollers are located in "Main" storyboard and you can set storyboardID in the Identity inspector)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let boxDescriptionNavVC = storyboard.instantiateViewControllerWithIdentifier(BoxDescriptionNavID) as! UINavigationController
let destinationTableViewVC = boxDescriptionNavVC.topViewController as! YourTableViewVC
destinationTableViewVC.textVariable = box.boxDescription
presentViewController(boxDescriptionNavVC, animated: true, completion: nil)