简体   繁体   English

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

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

I have two view controllers that both have a search bar and a table view.我有两个视图控制器,它们都有一个搜索栏和一个表格视图。 They have the same data but different cell layouts.它们具有相同的数据,但单元格布局不同。 The first view controller is SearchBarViewController and when a cell is selected to segues the next view controller, which is SearchesViewController . The first view controller is SearchBarViewController and when a cell is selected to segues the next view controller, which is SearchesViewController . I wan't to go back to SearchesViewController because they're need to be different cells.我不想 go 回到SearchesViewController因为它们需要是不同的单元格。

SearchBarViewController didselectRowAt: 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: SearchesViewController searchBarTextDidBeginEditing:

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

I keep getting a Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value in tableView.delegate = self .我不断收到 Thread 1: Fatal error: Unexpectedly found nil 同时在tableView.delegate = selfThread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value Any would be appreciated!任何将不胜感激!

where crash occurred:发生崩溃的地方:

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

Instead of present you need to pop controller而不是现在你需要弹出 controller

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问另一个视图控制器的功能时意外发现nil - unexpectedly found nil when accessing another view controller's functions 将View Controller用作辅助视图的委托时,展开可选项时失败,意外发现nil - Using View Controller as the delegate for an auxilliary view fails with unexpectedly found nil when unwrapping an optional 当尝试从表视图转到视图控制器时,我收到此错误:Unexpectedly found nil while unwrapping - When trying to segue to a view controller from a table view i get this error: Unexpectedly found nil while unwrapping 将UITabBarController设置为根视图控制器时,如何修复应用程序委托中的“意外找到nil” - How to fix “Unexpectedly found nil” in app Delegate when setting UITabBarController as root view controller 切换到另一个视图时显示导航栏 - displaying navigation bar when switching to another view 单击搜索栏时如何导航到另一个视图控制器 - How to navigate to another view controller when search bar is clicked 从另一个视图控制器 swift 重新加载表视图时发现 Nil 错误 - Found Nil Error when reload table view from another view controller swift 展开Optional值时意外发现nil - Unexpectedly found nil when unwrapping an Optional value 在 Swift 上运行应用程序时意外发现“nil” - Unexpectedly found 'nil' when running app on Swift 使用userDefaults时意外发现nil - unexpectedly found nil when use userDefaults
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM