【问题标题】:Programmatically replace UITableViewController by UIViewController with tableView inside via Button通过 Button 以编程方式将 UITableViewController 替换为 UIViewController 内的 tableView
【发布时间】:2019-01-15 00:40:11
【问题描述】:

我知道标题有点奇怪,但我尝试将 UITableViewController 替换为 UIViewController 并使用以下代码:

@objc func buttonAction(sender: UIButton!) {
    let newController = JobTableViewController()
    self.navigationController?.present(newController, animated: true, completion: nil)
}

但每次我点击此按钮时,我的ViewController 在此代码行中都会出现以下错误

tableView.delegate = self

线程 1:致命错误:打开包装时意外发现 nil 可选值

【问题讨论】:

  • 如果您使用故事板,那么您需要在storyboard 中连接tableView。如果以编程方式,则需要在访问之前对其进行初始化。
  • tableView.delegate = self写在viewController的什么地方?
  • @Abizern 在 ViewDidLoad 中,但我现在可以正常工作了,所以没关系,谢谢 :)

标签: ios swift uitableview uiviewcontroller delegates


【解决方案1】:

不是这个

let newController = JobTableViewController()

let newController = storyboard.instantiateViewController(withIdentifier: "VcId") as! JobTableViewController

你的vc在stroryboard中,不能用JobTableViewController(),只有当VC是通过程序创建的时候,别忘了在IB中给出VC的标识符VcId

【讨论】:

  • 这给了我以下错误Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
猜你喜欢
  • 2020-04-29
  • 1970-01-01
  • 2012-06-18
  • 2013-10-23
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多