Search code examples
uitableviewuisearchbardelegateappdelegate

How to reset UISearch from Appdelegate method applicationWillEnterForeground


I want to reset my UISearch when app is entering background, or entering foreground again. It would be enough when the tableview from UISearch gets hidden.

But when I try to hide it from AppDelegate.m it doesn't work. I have also logged the UIElements, there are also (null).

Here is how I try to access it:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
XLog(@"");
/*
 Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
 */

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    XLog(@"");
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController_iPad" bundle:[NSBundle mainBundle]];
} else {
    XLog(@"");
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController_iPhone" bundle:[NSBundle mainBundle]];
}


XLog(@"searchViewController.searchBar.text: %@", searchViewController.searchBar.text);
searchViewController.tableViewSearch.hidden = YES; // is (null)

XLog(@"searchViewController.tableViewSearch: %@", searchViewController.tableViewSearch); // is (null)

}

How can I access that? Do I something wrong here, or is it not allowed to access elements from other classes thru appdelegate.m?


Solution

  • NSArray *ary_navigationControllerViews = [[NSArray alloc] initWithArray:[self.navigationController viewControllers]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.class.description == %@", [[SearchViewController class] description]];
    NSArray *ary_viewController = [[NSArray alloc] initWithArray:[ary_navigationControllerViews filteredArrayUsingPredicate:predicate]];
    if ([ary_viewController count] > 0) 
    {
       SearchViewController *sVw =  (SearchViewController*) [ary_viewController objectAtIndex:0] ;
      sVw.tableViewSearch.hidden = YES;
    }
    

    You are initializing new instance of your view controller, instead you need to get existing instance of your view controller and hide your view. Hope this helps.