按下搜索栏时切换到另一个视图控制器时意外发现 nil

unexpectedly found nil when switching to another view controller when search bar is pressed

我有两个视图控制器,它们都有一个搜索栏和一个 table 视图。它们具有相同的数据,但单元格布局不同。第一个视图控制器是 SearchBarViewController,当一个单元格被选中时,下一个视图控制器是SearchesViewController。我不想回到 SearchesViewController 因为它们需要是不同的单元格。

SearchBarViewController didselectRowAt:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let searchesVC = storyboard?.instantiateViewController(withIdentifier: "SearchesViewController") as? SearchesViewController
    searchesVC?.search = [search[indexPath.row]]
    tableView.deselectRow(at: indexPath, animated: true)
    self.navigationController?.pushViewController(searchesVC!, animated: true)
    
}

SearchesViewController searchBarTextDidBeginEditing:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = false
    self.present(UINavigationController(rootViewController: SearchBarViewController()), animated: false, completion: nil)
    
}

我一直在 tableView.delegate = self 中得到 Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value。任何将不胜感激!

发生崩溃的地方:

   override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self //CRASH
    tableView.dataSource = self
    searchBar.delegate = self
    retriveData()
    print(search)
   }

你需要弹出控制器而不是 present

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = false
     self.navigationController?.popViewController(animated: true)
    
}