【发布时间】:2016-10-07 18:01:59
【问题描述】:
我在从 searchResultController 转移到另一个视图控制器时遇到问题
从一开始: 我有三个控制器,一个内嵌有 CollectionView 和 UISearchController 的 UIViewController,一个充当 searchResultController 的 UITableViewController 和一个最终的 UIViewController,每个 tableview 单元格在单击时都会继续。 Here is an image of Main.Storyboard。 Here the segue with its identifier is shown, which is the problem.
segue 链接自 SearchResultsTableViewController 本身,而不是 tableviewcell 单击搜索结果后,我会从 SearchResultsController 中的 didSelectRowAtIndexPath 方法执行该 Segue。但是,我收到“Receiver ... has no segue with identifier 'testSeg''”错误。
为清楚起见,控制器的名称及其角色为:
1)DiscoverViewController:(包含 UISearchController 和 CollectionView)
2) SearchResultsTableViewController(充当正确过滤和更新任何数据的searchResultsController。每个单元格都应该与此控制器分离)
3) UIViewController(一个简单的 UIViewController,它是 segue 的目的地)
问题在于转场,其他一切都按预期执行。这是 SearchResultsTableViewController 的代码。
注意:您可以忽略这是 Swift 3 的事实。我尝试使用 Swift 2.3 并得到相同的错误
private let reuseIdentifer = "searchCell"
class SearchResultsTableViewController: UITableViewController {
var searchResults: [String]!
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return (searchResults?.count)!
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifer, for: indexPath)
// Configure the cell...
cell.textLabel?.text = searchResults[indexPath.item]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "testSeg", sender: self)
}
}
searchResults 数组来自 DiscoverViewController。除了segue,一切都很好。选择一行后,应用程序崩溃并出现错误:
2016-10-07 12:17:51.216 MultiTestDemo[1028:10002] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'Receiver (<MultiTestDemo.SearchResultsTableViewController:
0x7fea214055f0>) has no segue with identifier 'testSeg''
我已确保 segue 标识符中没有空格或任何内容。我什至直接将它复制到 SearchResultsTableViewController 的 didselectrowatindexpath 函数中。
总而言之,我要求的是帮助,当我在 IB 中连接时,为什么 SearchResultsTableViewController 不能再识别 segue(或具有任何标识符的任何 segue)。任何帮助将不胜感激。
【问题讨论】:
-
如何实例化 SearchResultsTableViewController?
-
在您的 GitHub 链接中找到它。问题源于您如何实例化 SearchResultsTableViewController。这一行:
let searchResultsController = SearchResultsTableViewController()创建了它,但没有参考故事板——因此它对故事板中定义的 segue 一无所知。您应该改用 UIStoryboard 方法instantiateViewControllerWithIdentifier(如果需要,在情节提要中添加了一个标识符)。 -
这绝对有效。我之前实际上已经尝试过,但它继续崩溃,直到我删除了 searchREsultsController 中的重复实例化。谢谢你的回答。然而,似乎没有“回头路”。我意识到演示中没有导航控制器,但是如果说这是一个用户搜索功能,用户将如何返回搜索结果?我之前工作过的一个实现允许返回,但是当需要返回时使用 searchResultsController 会导致复杂化。
标签: ios swift uitableview segue uisearchcontroller