首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索模板tvOS

搜索模板tvOS
EN

Stack Overflow用户
提问于 2016-06-29 09:11:42
回答 2查看 869关注 0票数 3

有人知道如何实现搜索模板,如苹果的tvOS人机界面指南,使用本机开发目标-C或Swift,没有TVML?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-30 06:51:14

所以,经过研究,我找到了一个解决办法:

目标- C

如果在应用程序中是tabBar,那么我从UITabBarController (例如APTabBarController )创建了一个子类。在APTabBarController中,方法上

代码语言:javascript
复制
- (void)viewDidLoad

我接下来做的是:

代码语言:javascript
复制
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];

其中:

  1. 故事板-是我的故事板
  2. SearchResultsViewController -是我在故事板上的控制器,其中包含collectionView
  3. UISearchController -是允许查找所需内容的控制器。
  4. UISearchContainerViewController -而这些就像来自tabBarController的视图控制器
  5. 在"newTab“中-我添加了我需要的新创建的viewController

但是,我发现的问题是我找不到搜索的文本。为此,从UISearchController创建一个子类,并实现自定义

代码语言:javascript
复制
initWithViewController

在我的例子中,看起来是这样的:

In .h

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface SearchExercisesViewController : UISearchController

- (id) initWithViewController:(UIViewController *) viewController;

@end

In .m

代码语言:javascript
复制
#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

利润,现在,取代

代码语言:javascript
复制
UISearchController *searchController = [[UISearchController alloc] initWithViewController:myViewController];

使用

代码语言:javascript
复制
SearchExercisesViewController *searchController = [[SearchExercisesViewController alloc] initWithViewController:myViewController];

全都做完了。现在只剩下这些,将数据发送到包含集合视图的viewController,并实现用于搜索的逻辑。对于发送的数据,您可以代表模式或NSNotification。您可以在这篇文章中找到如何实现这一点:

it possible to Pass Data with popViewControllerAnimated?

Swift

在斯威夫特也是一样,如何做到这一点,你可以从苹果的例子中找到以下链接:

https://github.com/brunogb/TVExamples/tree/master/UIKitCatalogtvOSCreatingandCustomizingUIKitControls

票数 3
EN

Stack Overflow用户

发布于 2016-06-29 22:46:47

听起来你想看看UISearchController

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38095125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档