答案 0 :(得分:3)
因此,经过一天的研究,我找到了解决方案:
目标 - C
如果在应用程序中是tabBar,我从UITabBarController创建了一个子类,例如APTabBarController。在APTabBarController中,方法
- (void)viewDidLoad
我接下来会这样做:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SearchResultsViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"SearchResultsViewController"];
UISearchController *searchController = [[UISearchController alloc] initWithViewController:myViewController];
UISearchContainerViewController *containerVC = [[UISearchContainerViewController alloc] initWithSearchController: searchController];
containerVC.title = @"Search";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: containerVC];
NSMutableArray *newTab = [self.viewControllers mutableCopy];
[newTab addObject: navigationController];
[self setViewControllers: newTab];
其中:
但是,我发现的问题是我无法捕获搜索文本。为此,从UISearchController创建一个子类,并实现自定义
initWithViewController
就我而言,它看起来像这样:
在.h
#import <UIKit/UIKit.h>
@interface SearchExercisesViewController : UISearchController
- (id) initWithViewController:(UIViewController *) viewController;
@end
在.m
#import "SearchExercisesViewController.h"
@interface SearchExercisesViewController () <UISearchBarDelegate>
@property (nonatomic, strong) UIViewController *viewController;
@end
@implementation SearchExercisesViewController
- (id) initWithViewController:(UIViewController *) viewController {
self = [super initWithSearchResultsController:viewController];
if (self) {
self.viewController = viewController;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.searchBar.delegate = self;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"%@",searchText);
}
@end
获利,现在,替换
UISearchController *searchController = [[UISearchController alloc] initWithViewController:myViewController];
带
SearchExercisesViewController *searchController = [[SearchExercisesViewController alloc] initWithViewController:myViewController];
全部完成。现在只剩下就是将数据发送到包含集合视图的viewController,并实现搜索逻辑。对于发送的数据,您可以委派模式或NSNotification。您可以在该帖子中找到如何实现它:
it possible to Pass Data with popViewControllerAnimated?
<强>夫特强>
在swift中是一样的,怎么做,你可以从这些链接上找到Apple的例子:
答案 1 :(得分:0)
听起来你想看UISearchController
。