搜索模板tvOS

时间:2016-06-29 09:11:42

标签: objective-c swift tvos

任何人都知道如何在Apple tvOS人机界面指南中实现搜索模板,使用Objective-C或Swift中的本机开发,而不使用TVML?

2 个答案:

答案 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];

其中:

  1. 故事板 - 是我的故事板
  2. SearchResultsViewController - 是来自storyboard的控制器,包含collectionView
  3. UISearchController - 是一个控制器,可以找到你需要的东西
  4. UISearchContainerViewController - 这些就像是来自tabBarController的视图控制器
  5. 在“newTab”中 - 我添加了我需要的新创建的viewController
  6. 但是,我发现的问题是我无法捕获搜索文本。为此,从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的例子:

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

答案 1 :(得分:0)

听起来你想看UISearchController