搜索模板 tvOS
Search Template tvOS
有人知道如何在没有 TVML 的情况下使用 Objective-C 或 Swift 中的本机开发来实现 Apple tvOS 人机界面指南中的搜索模板吗?
听起来你想看看 UISearchController
。
所以,经过研究我找到了解决方案:
Objective - C
如果在应用程序中是 tabBar,我从 UITabBarController 创建了一个子类,例如APTabBar控制器。在 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];
其中:
- 故事板 - 是我的故事板
- SearchResultsViewController - 是我的故事板中包含 collectionView 的控制器
- UISearchController - 是允许找到你需要的东西的控制器
- UISearchContainerViewController - 这些就像来自 tabBarController
的视图控制器
- 在 "newTab" 中 - 我添加了我需要的新创建的 viewController
但是,我发现的问题是我无法捕获搜索到的文本。为此,从 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。您可以在 post:
中找到实现方法
it possible to Pass Data with popViewControllerAnimated?
Swift
在 swift 中是一样的,如何做到这一点,您可以从这些 link:
中找到有关 Apple 的示例
有人知道如何在没有 TVML 的情况下使用 Objective-C 或 Swift 中的本机开发来实现 Apple tvOS 人机界面指南中的搜索模板吗?
听起来你想看看 UISearchController
。
所以,经过研究我找到了解决方案:
Objective - C
如果在应用程序中是 tabBar,我从 UITabBarController 创建了一个子类,例如APTabBar控制器。在 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];
其中:
- 故事板 - 是我的故事板
- SearchResultsViewController - 是我的故事板中包含 collectionView 的控制器
- UISearchController - 是允许找到你需要的东西的控制器
- UISearchContainerViewController - 这些就像来自 tabBarController 的视图控制器
- 在 "newTab" 中 - 我添加了我需要的新创建的 viewController
但是,我发现的问题是我无法捕获搜索到的文本。为此,从 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。您可以在 post:
中找到实现方法it possible to Pass Data with popViewControllerAnimated?
Swift
在 swift 中是一样的,如何做到这一点,您可以从这些 link:
中找到有关 Apple 的示例