通过 xib 文件的 class 没有标识符 'searchSegue' 的 segue
Has no segue with identifier 'searchSegue' through xib file's class
我正在使用 segue(searchSegue
) 将我的搜索屏幕 (SearchViewController
) 连接到搜索结果页面 (PhotoStreamViewController
)。
SearchViewController
是集合视图。每个集合视图单元格在 Xib 文件中都有一个设计。 (Xib文件的class是SearchCategoryRowCell
)
目前,当我触发 searchSegue
到 SearchViewController
时,它工作正常。但是每当我通过 SearchCategoryRowCell
触发相同的 segue 时,我都会得到 Has no segue with identifier 'searchSegue'
SearchViewController.swift
class SearchViewController: UIViewController,UISearchBarDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
...
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchRedirect(keyword: searchBar.text!)
}
func searchRedirect(keyword:String) {
performSegue(withIdentifier: "searchSegue", sender:self)
PhotoStreamViewController.searchString = keyword
navigationController?.setNavigationBarHidden(false, animated: true)
}
...
}
SearchCategoryRowCell.swift
class SearchCategoryRowCell: UICollectionViewCell {
//the method below is triggered by a click action
@objc func searchRedirection(_ sender:UIButton) {
print("we")
print(subCategoryArray[sender.tag].name)
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
searchViewcontroller.searchRedirect(keyword: "otherSearchKeyword")
}
...
}
这个
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
不是当前呈现的 VC ,您必须在单元格 class
中使用委托或将 self 声明为 属性
class SearchCategoryRowCell: UICollectionViewCell {
var sear:SearchViewController?
}
ans 将其设置在 SearchViewController
中的 cellForItem 中
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! cellClassName
cell.sear = self
return cell;
}
//
那么你就可以使用呈现的对象了
@objc func searchRedirection(_ sender:UIButton) {
print("we")
print(subCategoryArray[sender.tag].name)
sear?.searchRedirect(keyword: "otherSearchKeyword")
}
你已经完美地解释了原因。你是说:
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
searchViewcontroller.searchRedirect(keyword: "otherSearchKeyword")
所以 searchViewcontroller
是一个全新的视图控制器,在您的代码中实例化。因此,它没有segues。带有 segues 的视图控制器是故事板中的视图控制器,它是一个完全不同的视图控制器。
我正在使用 segue(searchSegue
) 将我的搜索屏幕 (SearchViewController
) 连接到搜索结果页面 (PhotoStreamViewController
)。
SearchViewController
是集合视图。每个集合视图单元格在 Xib 文件中都有一个设计。 (Xib文件的class是SearchCategoryRowCell
)
目前,当我触发 searchSegue
到 SearchViewController
时,它工作正常。但是每当我通过 SearchCategoryRowCell
触发相同的 segue 时,我都会得到 Has no segue with identifier 'searchSegue'
SearchViewController.swift
class SearchViewController: UIViewController,UISearchBarDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
...
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchRedirect(keyword: searchBar.text!)
}
func searchRedirect(keyword:String) {
performSegue(withIdentifier: "searchSegue", sender:self)
PhotoStreamViewController.searchString = keyword
navigationController?.setNavigationBarHidden(false, animated: true)
}
...
}
SearchCategoryRowCell.swift
class SearchCategoryRowCell: UICollectionViewCell {
//the method below is triggered by a click action
@objc func searchRedirection(_ sender:UIButton) {
print("we")
print(subCategoryArray[sender.tag].name)
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
searchViewcontroller.searchRedirect(keyword: "otherSearchKeyword")
}
...
}
这个
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
不是当前呈现的 VC ,您必须在单元格 class
中使用委托或将 self 声明为 属性class SearchCategoryRowCell: UICollectionViewCell {
var sear:SearchViewController?
}
ans 将其设置在 SearchViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! cellClassName
cell.sear = self
return cell;
}
//
那么你就可以使用呈现的对象了
@objc func searchRedirection(_ sender:UIButton) {
print("we")
print(subCategoryArray[sender.tag].name)
sear?.searchRedirect(keyword: "otherSearchKeyword")
}
你已经完美地解释了原因。你是说:
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
searchViewcontroller.searchRedirect(keyword: "otherSearchKeyword")
所以 searchViewcontroller
是一个全新的视图控制器,在您的代码中实例化。因此,它没有segues。带有 segues 的视图控制器是故事板中的视图控制器,它是一个完全不同的视图控制器。