0

When I tap on the button, I want to bring up the Company Details view controller programmatically (using no storyboard), but I'm getting an NSException error for the button's function. I believe CompanyDetailsViewController() is null and I'm not sure why because I created a class called CompanyDetailsViewController

func detailsButtonTapped(sender: UIButton!) {
    let companyDetailsViewController = CompanyDetailsViewController()
    self.navigationController?.pushViewController(companyDetailsViewController, animated: true)
}

Company Details View Controller Class

import UIKit

class CompanyDetailsViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
 var tableView: UITableView  =   UITableView()
 let animals : [String] = ["Dogs","Cats","Mice"]

  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.blue
  }

  //code for create table view, etc
}
14wml
  • 4,048
  • 11
  • 49
  • 97
  • (1) Why not use the modern tools (2) Possibly a duplicate of http://stackoverflow.com/questions/2688659/how-do-i-create-a-uiviewcontroller-programmatically – SwiftArchitect Jan 07 '17 at 02:25
  • for me your code looks ok. – Ashok R Jan 07 '17 at 02:45
  • Swift - programmatically navigate to another view controller/scene: http://stackoverflow.com/questions/27374759/swift-programmatically-navigate-to-another-view-controller-scene – Ashok R Jan 07 '17 at 02:46
  • @SwiftArchitect i am new to iOS. when you say why not use the modern tools are you referring to storyboard? – Ashok R Jan 07 '17 at 02:49
  • Absolutely - If you are new, one more reason to create world class software with word class tool. – SwiftArchitect Jan 07 '17 at 03:17
  • @Ashok The question you provided is for navigating to another view controller using storyboard. I DO NOT want to use storyboard – 14wml Jan 07 '17 at 03:25
  • @SwiftArchitect Hi thanks for replying! (1) I'm not using storyboard because it's a group app so even one person's error on storyboard would be extremely hard to find and fix especially using github (2) Unfortunately that answer is in Objective C and it would be great if you had a Swift 3.0 answer – 14wml Jan 07 '17 at 03:28

1 Answers1

0

Solved! It was an error with the button's selector call because I didn't know the selector call was changed with Swift 3.

class CompanyViewController: UIViewController {
    override func viewDidLoad() {
        button.addTarget(self, action: #selector(CompanyViewController.detailsButtonTapped(button:)), for: .touchUpInside)    
    }


    func detailsButtonTapped(button: UIButton!) {
        let companyDetailsViewController = CompanyDetailsViewController()
        self.navigationController?.pushViewController(companyDetailsViewController, animated: true)
    }
}
14wml
  • 4,048
  • 11
  • 49
  • 97