2

我有一個標籤欄項目連接導航控制器與UIViewController作爲根視圖控制器。標籤欄項目上的第一次觸摸切換到該視圖。第二個觸摸會彈出到根視圖控制器。第三次觸摸確實是而不是滾動到頂部。標籤欄項目第三次觸摸表格視圖不滾動到頂部

我在其他應用程序中看到了這種滾動到頂部的行爲,但在搜索網頁後,我找不到任何有關它的信息。

這是滾動視圖或表格視圖附加到標籤欄項目的默認行爲,還是我需要實現自己的東西?

回答

1

不,這不是默認行爲,你必須自己實現它。

我會通過讓應用程序委託標籤欄控制器的委託並實現-tabBarController:didSelectViewController來發布通知。傾聽你的表​​視圖控制器通知,並做一些事情,如:

if (self == self.navigationController.topViewController) 
    [self.tableView scrollToTop]; 
+0

我不想滾動到頂部,如果標籤欄項觸摸導致視圖切換。我如何判斷我是否正在切換視圖?希望比'-tabBarController:shouldSelectViewController:'更好。 –

+0

不,我認爲在-shouldSelect中測試當前選定的選項卡......可能是你最好的選擇:( – hatfinch

+0

好的,程序員必須做一個程序員必須做的事情,謝謝你的幫助 –

2

這裏是被點擊

當標籤欄在AppDelegate中設置的TabBar委託

滾動到表視圖頂部的解決方案
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 
    if (tabBarController.selectedIndex == 0) { 

     UINavigationController *selectedNav = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex]; 
     UIViewController *currentVC = selectedNav.visibleViewController; 
     if([currentVC isMemberOfClass:NSClassFromString(@"HomeViewController")]) 
     { 

      [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:nil]; 
     } 
    } 
    return YES; 
} 

在HomeViewController.m鑑於沒有負載監聽通知

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(refreshView:) 
               name:@"refreshView" 
               object:nil]; 

刷新方法

-(void)refreshView:(NSNotification *) notification{ 
     if (self == self.navigationController.topViewController) 
      [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; 
    } 
+0

給我一個錯誤:'Property'在'AppDelegate'類型的對象上找不到'tabBarController' –

0

由於您的選項卡控制器只能有一個代表,你可能想看看答案this question,其中介紹瞭如何監聽使用KVO水龍頭。

3

我意識到這是一個老問題,但我也想創建這種行爲,我想我有一個更簡單的解決方案。

首先,將您的AppDelegate設置爲您的UITabBarController的代表。那麼這種方法添加到AppDelegate.m:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 
    if ([tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex]==viewController) 
    { 
     if ([viewController isKindOfClass:[UITableViewController class]]) 
     { 
      [[(UITableViewController *)viewController tableView] setContentOffset:CGPointZero animated:YES]; 
     } 
     else if ([viewController isKindOfClass:[UINavigationController class]]) 
     { 
      UINavigationController *nav = (UINavigationController *)viewController; 
      if ([nav.visibleViewController isKindOfClass:[UITableViewController class]]) 
       [[(UITableViewController *)nav.visibleViewController tableView] setContentOffset:CGPointZero animated:YES]; 
     } 
    } 

    return YES; 
} 

,如果你的標籤點,一個UITableViewController或與一個UITableViewController作爲根視圖一個UINavigationController,你不必擔心它的UITableViewController區分這工作受影響,發送通知等。

+0

而不是'setContentOffset',我建議使用 'scrollToRowAtIndexPath:[NSIndexPath] indexPathForItem:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];' –

相關問題