[英]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 = self
中Thread 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.