0

Language is Swift 4

I have a tableView which is showing some categories and below each category it has one or more detail rows. When I click on the detail row, I want it to launch another tableView to show the selected detail row, along with some additional information pertaining to the selected detail row.

To this end, I have implemented the following function:

// launch the detail view controller
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let detailView = DetailVC()
    detailView.customInit(detailID: tempViewData[indexPath.section].detailIDs[indexPath.row])
    setupTableView.deselectRow(at: indexPath, animated: true)
    self.navigationController?.pushViewController(detailView, animated: true)
}

However, when I run the app, while no errors are listed, the detail view does not launch. What am I doing wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
CMan
  • 71
  • 1
  • 7
  • 1
    Did you put this viewController in a UINavigationController and set the setupTableView's delegate to this viewController – ZHZ Nov 01 '17 at 00:40
  • I'm not totally certain what you're asking. In this view controller's viewDidLoad function, I have added: setupTableView.delegate = self – CMan Nov 01 '17 at 00:51
  • but it doesn't display the detail view controller. – CMan Nov 01 '17 at 00:52
  • Then the most likely cause is that `self.navigationController` is `nil` - Have you embedded your view controller in a navigation controller? – Paulw11 Nov 01 '17 at 01:00
  • Is this `didSelectRowAt` method being called? If so, if `self.navigationController` a non-nil value? – rmaddy Nov 01 '17 at 01:01
  • I have not embedded the setupTableView controller in a navigation controller. I setup the UI using the storyboard and have created 3 view controllers, but no navigation controller. I'm using a segue to move from the first view controller (which is just a screen with some buttons on it) to the setupVC, and this works properly. I have a navigation bar at the top to provide a Back button. I tried using a segue in place of the self.navigationController?.pushViewController line, but that resulted in an error. Do I need to embed everything in a navigation controller? – CMan Nov 01 '17 at 01:18

2 Answers2

0

Select your first viewController in Storyboard, then embed it into a UINavigationController from the menu like this. enter image description here

ZHZ
  • 2,088
  • 13
  • 16
  • This worked, however now from my initial view controller, when I click on the button to display the SetupVC, it displays it for a second, then it displays it again. Why would it do this? – CMan Nov 01 '17 at 19:24
  • Ok, I resolved the problem. On my initial view controller I had setup a segue to the SetupVC. I then had created an action outlet from the same button and called the segue function. I commented out this segue call and things are working properly now. – CMan Nov 01 '17 at 20:37
0

Use self.storyboard?.instantiateViewController(withIdentifier:kCreateFBAccountVCID ) as! DetailVC Instead of DetailVC()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let detailView = **self.storyboard?.instantiateViewController(withIdentifier:kCreateFBAccountVCID ) as! DetailVC**
detailView.customInit(detailID: tempViewData[indexPath.section].detailIDs[indexPath.row])
setupTableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.pushViewController(detailView, animated: true)}
Sandip Gill
  • 1,060
  • 1
  • 11
  • 22