【问题标题】:unexpectedly found nil when switching to another view controller when search bar is pressed按下搜索栏时切换到另一个视图控制器时意外发现 nil
【发布时间】:2020-10-14 07:40:07
【问题描述】:

我有两个视图控制器,它们都有一个搜索栏和一个表格视图。它们具有相同的数据,但单元格布局不同。 The first view controller is SearchBarViewController and when a cell is selected to segues the next view controller, which is 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)
   }

【问题讨论】:

    标签: ios swift xcode uiviewcontroller uicollectionview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多