2012-07-30 25 views
0

我想在應用程序進入後臺或重新輸入前景時重置我的UISearch。當UISearch的tableview被隱藏時就足夠了。如何從Appdelegate方法應用程序重置UISearchWillEnterForeground

但是,當我試圖從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訪問其他類的元素?

回答

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

您正在初始化視圖控制器的新實例,而不是您需要獲取視圖控制器的現有實例並隱藏視圖。希望這可以幫助。

相關問題