0

Instead of assigning the view controller as the UITableViewDelegate, I'm trying to reduce the code in the view controller by creating an extension for the UITableViewDelegate.

Why am I getting the error "Use of unresolved identifier companyDetailVC" for the line companyDetailsVC = CompanyDetailsViewController() when that is correct Swift 3 syntax?

Code

extension TableViewDelegate: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        companyDetailsVC = CompanyDetailsViewController()
        self.present(companyDetailsVC, animated: true, completion: nil)
    } 
}

Edit: I'm trying to do this programmatically without storyboard. I created a UITableViewDelegate extension because I'm trying to reduce the code in the View Controller.

14wml
  • 4,048
  • 11
  • 49
  • 97

2 Answers2

1

The code for presenting the Viewcontroller should be somewhat like this and the extension should be like

extension YourClassNameHere: UITableViewDelegate {
    //then your did select method comes here and in that put this code for presenting the viewcxontroller
    let companyDetailsVC = CompanyDetailsViewController() //change this to your class name
    self.present(companyDetailsVC, animated: true, completion: nil)
}
Sai Li
  • 685
  • 6
  • 14
0

Replace your code with below code. You will need to provide your ViewController to create its extension and to use the property and method of it.

Make sure companyDetailsVC is declared in the controller. You don't need to use self until its called from block.

extension yourViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.companyDetailsVC = CompanyDetailsViewController()
    self.present(companyDetailsVC, animated: true, completion: nil)
  }    
}
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71