0

I started with the default Tabbed Application, added some tabs in the Storyboards with their own viewcontrollers, how can I know when a tab that's already selected, get's touched again?

Tab 1 goes to a webview that has loaded other pages, when the user hits the home tab again, when it's still highlighted, I'd like to reload the initial url where it started.

Thanks for any ideas!

Mark
  • 555
  • 6
  • 17

3 Answers3

0

The UITabBarControllerDelegate method [– tabBarController:didSelectViewController:] gets called each time the tab bar is touched. The documentation for this API states:

In iOS v3.0 and later, this (selected view controller) could be the same view controller that was already selected.

So if you detect your specified tab being selected again, you can have this delegate method reload your initial URL.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I'm trying to add a delegate to the TabBarController from the default storyboard, and also when I changed the class to point to a `code`CustomTabBarController. But the method of dragging from the delegate from the Outlets section isn't working. and I'm trying to trigger this - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"Tab touched"); } – Mark May 17 '13 at 04:12
  • You need to designate *something* as your delegate, so whatever object that is, in it's .h ("`@interface`") file, you need to add "`UITabBarControllerDelegate`" to it's declaration. – Michael Dautermann May 17 '13 at 04:17
  • I have it in @interface CustomUITabBarController : UITabBarController – Mark May 17 '13 at 04:28
  • and is the custom class of your tab bar controller in your storyboard set as "`CustomUITabBarController`"? – Michael Dautermann May 17 '13 at 04:29
  • oops, thought I was on to something when I dragged the delegate from the Tab Bar to the CustomTabBarController above it... error:'NSInternalInconsistencyException', reason: 'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.' *** First throw call stack: – Mark May 17 '13 at 04:39
  • Thanks for your help. I got it, I needed this in my AppDelegate.m self.tabBarController = (UITabBarController*)self.window.rootViewController; self.tabBarController.delegate=self; – Mark May 17 '13 at 06:28
0
@interface AHTabBarController () <UITabBarControllerDelegate>
@property (nonatomic, strong) UIViewController* previousViewController;
@end

///

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isEqual:self.previousViewController])
    {
          NSLog(@"reselect tabbar");
    }
    self.previousViewController = viewController;
}
vplusm
  • 1
  • 1
0

Here is a full answer for common use cases.


  1. Create a protocol for handling the reselection

    protocol TabInteractionDelegate {
        func didReselectTab(at index: Int, with item: UITabBarItem)
    }
    
  2. Call the protocol from a Custom UITabBarController

    class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
    
        var tabInteractionDelegate: TabInteractionDelegate?
    
        // ...
    
        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    
            // This will:
            // 1. Call when the tab is reselected (i.e. The tab does not switch)
            // 2. NOT get called when the tab is switching to a new tab and showing a new view controller
    
            if (tabBar.items?[selectedIndex] == item) {
                 tabInteractionDelegate?.didReselectTab(at: selectedIndex, with: item)
            }
         }
    
    }
    
  3. Listen to changed in the UIViewController you need it in

    class CustomViewController: UIViewController, TabInteractionDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Attach the delegate 
            if let tabBarCont = tabBarController as? ChoiceTabBarController {
                tabBarCont.tabInteractionDelegate = self
            }
    
        }
    
        // Listen to the change 
        func didReselectTab(at index: Int, with item: UITabBarItem) {
            print("\(index) :: \(item)")
    
            // Here you can grab your scrollview and scroll to top or something else
        }
    
    }
    
Michael
  • 9,639
  • 3
  • 64
  • 69