当我点击按钮时,我想以编程方式显示公司详细信息视图控制器 (不使用故事板) ,但我获得了NSException按钮功能的错误。我相信CompanyDetailsViewController()
为空,我不确定为什么,因为我创建了一个名为CompanyDetailsViewController
func detailsButtonTapped(sender: UIButton!) {
let companyDetailsViewController = CompanyDetailsViewController()
self.navigationController?.pushViewController(companyDetailsViewController, animated: true)
}
公司详情查看控制器类
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
}
答案 0 :(得分:0)
解决!按钮的选择器调用出错,因为我不知道选择器调用是用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)
}
}