我想在应用进入后台或重新进入前景时重置我的UISearch。当UISearch的tableview被隐藏时就足够了。
但是当我试图从AppDelegate.m隐藏它时,它不起作用。我也记录了UIElements,也有(null)。
以下是我尝试访问它的方法:
- (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)
}
我该如何访问?我在这里做错了什么,或者不允许通过appdelegate.m访问其他类的元素?
答案 0 :(得分:2)
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;
}
您正在初始化视图控制器的新实例,而是需要获取视图控制器的现有实例并隐藏视图。希望这可以帮助。