-2

i've a problem with a search bar... I always use this code and it works until sdk 9...if i compile with sdk 8 it works also on iOS 9... The error is:

2016-01-04 18:28:48.335 Project[1241:20753] -[VersioniViewController_TableResults topViewController]: unrecognized selector sent to instance 0x7c0b8fb0 2016-01-04 18:28:48.339 Project[1241:20753] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[VersioniViewController_TableResults topViewController]: unrecognized selector sent to instance 0x7c0b8fb0'

My code is:

#import <UIKit/UIKit.h>

@interface VersioniViewController_TableResults : UITableViewController

@property (nonatomic, strong) NSArray *versioni;

@end



#import "VersioniViewController_TableResults.h"
#import "VersioniDetailViewController.h"
#import "SearchVersioniViewController.h"
#import "Versioni.h"

#define ENABLE_SCOPE_BUTTONS 1


@interface VersioniViewController_TableResults () <UISearchResultsUpdating, UISearchBarDelegate>

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResultsVersioni; //Filtered search results

@end

#pragma mark -

@implementation VersioniViewController_TableResults

- (void)viewDidLoad {

[super viewDidLoad];

self.versioni = [Versioni allVersioni];

// Create a mutable array to contain products for the search results table.
self.searchResultsVersioni = [NSMutableArray arrayWithCapacity:[self.versioni count]];

// The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
 UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"versioniController"];

self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

self.searchController.searchResultsUpdater = self;

self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);

self.tableView.tableHeaderView = self.searchController.searchBar;

#if ENABLE_SCOPE_BUTTONS

NSMutableArray *scopeButtonTitles = [[NSMutableArray alloc] init];
[scopeButtonTitles addObject:NSLocalizedString(@"All", @"Search display controller All button.")];

for (NSString *deviceType in [Versioni deviceTypeNames]) {
    NSString *displayName = [Versioni displayNameForType:deviceType];
    [scopeButtonTitles addObject:displayName];
}

self.searchController.searchBar.scopeButtonTitles = scopeButtonTitles;
self.searchController.searchBar.delegate = self;

#endif

self.definesPresentationContext = YES;

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"pushDetailVersioni"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    Versioni *versioni = self.versioni[indexPath.row];

    VersioniDetailViewController *destinationController = segue.destinationViewController;
    destinationController.versioni = versioni;
}

}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [self.versioni count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ProductCell";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Versioni *versioni = [self.versioni objectAtIndex:indexPath.row];
cell.textLabel.text = versioni.name;
return cell;
 }


 #pragma mark - UISearchResultsUpdating

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {

NSString *searchString = [self.searchController.searchBar text];

NSString *scope = nil;

NSInteger selectedScopeButtonIndex = [self.searchController.searchBar selectedScopeButtonIndex];
if (selectedScopeButtonIndex > 0) {
    scope = [[Versioni deviceTypeNames] objectAtIndex:(selectedScopeButtonIndex - 1)];
}

[self updateFilteredContentForProductName:searchString type:scope];

if (self.searchController.searchResultsController) {
    UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;

    SearchVersioniViewController *vdvc = (SearchVersioniViewController *)navController.topViewController;
    vdvc.searchResultsVersioni = self.searchResultsVersioni;
    [vdvc.tableView reloadData];
}

}

 #pragma mark - UISearchBarDelegate

 // Workaround for bug: -updateSearchResultsForSearchController: is not called when scope buttons change
 - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
[self updateSearchResultsForSearchController:self.searchController];
}


 #pragma mark - Content Filtering

 - (void)updateFilteredContentForProductName:(NSString *)productName type:(NSString *)typeName {

if ((productName == nil) || [productName length] == 0) {
    // If there is no search string and the scope is "All".
    if (typeName == nil) {
        self.searchResultsVersioni = [self.versioni mutableCopy];
    } else {
        // If there is no search string and the scope is chosen.
        NSMutableArray *searchResults = [[NSMutableArray alloc] init];
        for (Versioni *versioni in self.versioni) {
            if ([versioni.type isEqualToString:typeName]) {
                [searchResults addObject:versioni];
            }
        }
        self.searchResultsVersioni = searchResults;
    }
    return;
}


[self.searchResultsVersioni removeAllObjects]; // First clear the filtered array.

/*  Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
 */
for (Versioni *versioni in self.versioni) {
    if ((typeName == nil) || [versioni.type isEqualToString:typeName]) {
        NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
        NSRange productNameRange = NSMakeRange(0, versioni.name.length);
        NSRange foundRange = [versioni.name rangeOfString:productName options:searchOptions range:productNameRange];
        if (foundRange.length > 0) {
            [self.searchResultsVersioni addObject:versioni];
        }
    }
}
}

@end
Andrea Culot
  • 114
  • 2
  • 9
  • SearchVersioniViewController *vdvc = (SearchVersioniViewController *)navController.topViewController; is wrong – Teja Nandamuri Jan 04 '16 at 18:59
  • it sgould be SearchVersioniViewController *vdvc = (uinavigationcontroller *)navController.topViewController; – Teja Nandamuri Jan 04 '16 at 18:59
  • Possible duplicate of [unrecognized selector sent to instance](http://stackoverflow.com/questions/2455161/unrecognized-selector-sent-to-instance) – Cristik Jan 04 '16 at 19:06

1 Answers1

0

Your issue is caused by this line:

UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;

You are mistakenly assuming that the searchResultsController is a UINavigationController when in fact it is your own VersioniViewController_TableResults.

It's really hard to tell what you have or what you are really trying to get but perhaps you can get the navigation controller from the resulting VersioniViewController_TableResults.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you a lot. I found the problem that was different but you put me on the right track. – Andrea Culot Jan 04 '16 at 22:47
  • 1
    Remember to post an answer of the correct solution to this question for other people with the same problem. You should accept the correct answer not the one which helped you find the correct answer. – sam_smith Aug 12 '16 at 00:52