3

Anybody know how to implement search template like in Apple tvOS Human Interface Guidelines, using native development in Objective-C or Swift, without TVML ?

Alexandr Pavlov
  • 411
  • 3
  • 12

2 Answers2

3

So, after research I was able to find a solution:

Objective - C

If in application is tabBar, i created a subclass from UITabBarController e.g. APTabBarController. In APTabBarController, in method

- (void)viewDidLoad

I do next:

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

Where:

  1. storyboard - is my storyboard
  2. SearchResultsViewController - is my controller from storyboard that contains collectionView
  3. UISearchController - is controller that allow to find what do you need
  4. UISearchContainerViewController - and these one is like a view controller from tabBarController
  5. In "newTab" - I add fresh created viewController that i need

But, problem that I found is that i can't catch searched text. For that, create a subclass from UISearchController, and implement custom

initWithViewController

In my case it looks like these:

In .h

#import <UIKit/UIKit.h>

@interface SearchExercisesViewController : UISearchController

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

@end

In .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

Profit, and now, replace

UISearchController *searchController = [[UISearchController alloc] initWithViewController:myViewController];

with

SearchExercisesViewController *searchController = [[SearchExercisesViewController alloc] initWithViewController:myViewController];

All done. Now only that remain is to sent data to viewController that contains collection view, and implement logic for search. For sent data you can you Delegate pattern or NSNotification. You can find how to implement that in that post:

it possible to Pass Data with popViewControllerAnimated?

Swift

In swift is the same, how to do that, you can find on Apple example from these link:

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

Alexandr Pavlov
  • 411
  • 3
  • 12
0

Sounds like you want to look at UISearchController.

Justin Voss
  • 6,294
  • 6
  • 35
  • 39