Swift 搜索结果中的搜索结果控制器转至另一个视图控制器
Swift Search Result Controller in search results segue to another view controller
问题:
我有一个 table 视图,用户可以滚动查找内容或使用搜索栏。搜索栏不是使用 StoryBoard 创建的。我的视图有一个 UISearchController
来处理搜索栏和搜索结果更新。我 运行 遇到的问题是,由于我的 SearchResultsController
是由另一个控制器实例化的,所以我无法执行到另一个视图的 segue,或者至少我找不到解决方案。除了我的搜索结果控制器和它指定的视图之间的 segue 之外,一切正常。
我想做什么
让我的 MyNavTableViewController
代表参加 MySearchResultsController
。在搜索结果控制器中,当用户点击 TableViewCell
时,它将转到另一个视图。我没有使用 StoryBoard 来完成此操作,我很难使用 segues 转换到另一个视图。
如果我不能让它工作我可能会做的事情:
我必须在视图之间传递信息,对我来说,我总是使用 segues 来完成它。但是,如果这不起作用,我可能会尝试通过将视图推送到导航控制器堆栈并将数据写入共享数据库或其他内容来以模态方式呈现视图。不过,我更愿意使用 segue。
研究:
肯定不止这些,但我不会在 url 上占用太多 space。
Creating a segue programmatically
我的设置
我将尽量保持简洁。代码比我显示的要多。我只是想尝试清理它,这样我就只展示重要的东西。我还更改了一些名称,因为可能包含敏感信息。这可能没什么大不了的,但我宁愿安全。
class MyNavTableViewController: UIViewController, UITableViewDataSource{
//this is
@IBOutlet weak var tableView: UITableView!
var searchController: UISearchController!
override func viewDidLoad(){
...code
tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: tblId)
let resultsController = MySearchResultsController()
resultsController.databaseFilePath = databaseFilePath()
//this is essential that I use a segue because between my views I'm passing information between them.
resultsController.photo = photo!
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = searchBarPlaceHolderText
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController
}
}
MySearchResultsController: UITableViewController, UISearchResultsUpdating {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
//self.performSegueWithIdentifier(imagePrepareStoryBoardId, sender: tableView.cellForRowAtIndexPath(indexPath))
/*let imagePrepare = ImagePrepareController()
customSegue = SearchResultSegue(identifier: imagePrepareId, source: self, destination: imagePrepare)*/
//neither storyboard nor navigationController can be nil.
let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)
//destVC.photo = photo!
//self.presentViewController(destVC, animated: false, completion: nil)
self.navigationController!.pushViewController(destVC, animated: false)
}
}
我失败的尝试
1) 直接 segue - 不起作用,因为 MySearchResultsController 不是情节提要中的视图。从我读到的一切来看,转场只能在 SB 中创建。
2) 将视图推送到导航堆栈。唯一的问题是我无法在视图之间发送数据(或者至少从我读过的内容)。我也在这个断点得到这个错误:
let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)
fatal error: unexpectedly found nil while unwrapping an Optional value
我仔细检查了 imagePrepareStoryBoardId。没错。
3) 使用自定义 segue - 正如您从注释掉的行中看到的那样,这也不起作用。我一直在 SB 中使用 segues,所以这种方法对我来说有点新鲜。我可能在某个地方搞砸了。
首先创建一个协议
protocol SelectedCellProtocol {
func didSelectedCell(text: String)
}
在你的 UITableViewClass 上声明它
class MySearchResultsController: UITableViewController, UISearchResultsUpdating {
var delegate:SelectedCellProtocol?
}
并在选定的单元格方法上调用它:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.delegate?.didSelectedCell(cell.textLabel?.text)
}
声明结果控制器时,设置委托
let resultsController = MySearchResultsController()
resultsController.databaseFilePath = databaseFilePath()
//this is essential that I use a segue because between my views I'm passing information between them.
resultsController.photo = photo!
resultsController.delegate = self
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = searchBarPlaceHolderText
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController
然后执行协议
class MyNavTableViewController: UIViewController, UITableViewDataSource, SelectedCellProtocol{
func didSelectedCell(text: String) {
print(text)
//make your custom segue
}
}
问题:
我有一个 table 视图,用户可以滚动查找内容或使用搜索栏。搜索栏不是使用 StoryBoard 创建的。我的视图有一个 UISearchController
来处理搜索栏和搜索结果更新。我 运行 遇到的问题是,由于我的 SearchResultsController
是由另一个控制器实例化的,所以我无法执行到另一个视图的 segue,或者至少我找不到解决方案。除了我的搜索结果控制器和它指定的视图之间的 segue 之外,一切正常。
我想做什么
让我的 MyNavTableViewController
代表参加 MySearchResultsController
。在搜索结果控制器中,当用户点击 TableViewCell
时,它将转到另一个视图。我没有使用 StoryBoard 来完成此操作,我很难使用 segues 转换到另一个视图。
如果我不能让它工作我可能会做的事情:
我必须在视图之间传递信息,对我来说,我总是使用 segues 来完成它。但是,如果这不起作用,我可能会尝试通过将视图推送到导航控制器堆栈并将数据写入共享数据库或其他内容来以模态方式呈现视图。不过,我更愿意使用 segue。
研究: 肯定不止这些,但我不会在 url 上占用太多 space。
Creating a segue programmatically
我的设置
我将尽量保持简洁。代码比我显示的要多。我只是想尝试清理它,这样我就只展示重要的东西。我还更改了一些名称,因为可能包含敏感信息。这可能没什么大不了的,但我宁愿安全。
class MyNavTableViewController: UIViewController, UITableViewDataSource{
//this is
@IBOutlet weak var tableView: UITableView!
var searchController: UISearchController!
override func viewDidLoad(){
...code
tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: tblId)
let resultsController = MySearchResultsController()
resultsController.databaseFilePath = databaseFilePath()
//this is essential that I use a segue because between my views I'm passing information between them.
resultsController.photo = photo!
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = searchBarPlaceHolderText
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController
}
}
MySearchResultsController: UITableViewController, UISearchResultsUpdating {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
//self.performSegueWithIdentifier(imagePrepareStoryBoardId, sender: tableView.cellForRowAtIndexPath(indexPath))
/*let imagePrepare = ImagePrepareController()
customSegue = SearchResultSegue(identifier: imagePrepareId, source: self, destination: imagePrepare)*/
//neither storyboard nor navigationController can be nil.
let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)
//destVC.photo = photo!
//self.presentViewController(destVC, animated: false, completion: nil)
self.navigationController!.pushViewController(destVC, animated: false)
}
}
我失败的尝试
1) 直接 segue - 不起作用,因为 MySearchResultsController 不是情节提要中的视图。从我读到的一切来看,转场只能在 SB 中创建。
2) 将视图推送到导航堆栈。唯一的问题是我无法在视图之间发送数据(或者至少从我读过的内容)。我也在这个断点得到这个错误:
let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)
fatal error: unexpectedly found nil while unwrapping an Optional value
我仔细检查了 imagePrepareStoryBoardId。没错。
3) 使用自定义 segue - 正如您从注释掉的行中看到的那样,这也不起作用。我一直在 SB 中使用 segues,所以这种方法对我来说有点新鲜。我可能在某个地方搞砸了。
首先创建一个协议
protocol SelectedCellProtocol {
func didSelectedCell(text: String)
}
在你的 UITableViewClass 上声明它
class MySearchResultsController: UITableViewController, UISearchResultsUpdating {
var delegate:SelectedCellProtocol?
}
并在选定的单元格方法上调用它:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.delegate?.didSelectedCell(cell.textLabel?.text)
}
声明结果控制器时,设置委托
let resultsController = MySearchResultsController()
resultsController.databaseFilePath = databaseFilePath()
//this is essential that I use a segue because between my views I'm passing information between them.
resultsController.photo = photo!
resultsController.delegate = self
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = searchBarPlaceHolderText
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController
然后执行协议
class MyNavTableViewController: UIViewController, UITableViewDataSource, SelectedCellProtocol{
func didSelectedCell(text: String) {
print(text)
//make your custom segue
}
}