Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Clicking on UITabBarControllers Tab goes to root of Navigation controller

I have a UITabBarController setup with 2 UINavigationControllers.

One UINavigationController has One UIViewController, the other UINavigationController has Two UIViewControllers. If you then navigate to the second UIViewController and click the Tab that is already selected it bring you to the root of the UINavigationController (This would be the first UIViewController).

Is there a way to stop this from happening? I do not want the user to be able to click an already selected Tab to go to the root of the Navigation Controller.

like image 792
Mausimo Avatar asked Jan 31 '11 21:01

Mausimo


People also ask

What is a tab bar view controller?

Overview. Tab bar controllers are implemented by the UITabBarController class. They allow a user of to switch between multiple arbitrary view controllers by maintaining an array of UIViewControllers .

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


1 Answers

To do this you need to implement a function in your app delegate to pick up the tabbar delegate calls.

In your app delegate.m file, in the didfinishlaunching method, add this line

[tabBarController setDelegate:self];

then implement this method (also in your app delegate):

- (BOOL)tabBarController:(UITabBarController *)theTabBarController shouldSelectViewController:(UIViewController *)viewController
{
  return (theTabBarController.selectedViewController != viewController);
}

This gets called as part of the tab delegate protocol and will stop the selection of a tab if its already the selected one.

Hope that helps.

like image 100
TimMedcalf Avatar answered Oct 15 '22 06:10

TimMedcalf