Push detail controller from search display controller's table view
-
06-05-2021 - |
Pregunta
I implemented a search option in my app but when I search something, I can't push a detail controller from the search display controller's table view.
Is there a way to do that?
Thank you so much!
No hay solución correcta
Otros consejos
We'll assume you have a UISearchBarDelegate
method in a UIViewController
called SearchViewController. To push (DetailViewController*) dvc
, implement these methods in SearchViewController:
#pragma mark - Search bar delegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchTextMatchedSomeTarget) {
textForDetailView = searchText;
[self performSegueWithIdentifier:seguePushDetailView sender:self];
}
}
#pragma mark - View lifecycle
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:seguePushDetailView]) {
dvc = (DetailViewController *)[segue destinationViewController];
dvc.delegate = (id)self;
dvc.searchText = textForDetailView;
}
}
#pragma mark - Detail view controller protocol delegate
- (void)DetailViewControllerDidFinish:(DetailViewController *)controller
{
NSString *somethingBackFromDetailView = controller.backToSearchView;
}
UIDetailViewController
declares a protocol with the "did finish" method and properties of whatever type you like, both to receive data from SearchViewController and to send back any data after any detail processing. The segue is added in Xcode IB by control dragging from UISearchViewController
to UIDetailViewController
. NB: the segue source and destination are the view controllers themselves. That way they can be invoked programmatically as opposed to automatic invocation on a tap event.