make UITabBar forget last state - iphone

When you have a UINavigationController integrated into one of the Tabs of a UITabBarController, the UITabBarController remembers where the user was last time the tab was used. Is it possible to have the UITabBarController forget and always start the tab as if it was the first time?
f.e.
structure is a follows
Tab1
View1
View2
Tab2
View1
The user starts at Tab1/View1, then he navigates to Tab1/View2. He changes to Tab2/View1 and then presses Tab1:
Current behavior: he appears at Tab1/View2
Wanted behaviour: he appears at Tab1/View1

Try like below it will help you
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
for(int i = 0; i < tabBarController.viewControllers.count; i++) {
if(tabBarController.selectedIndex != i && [[tabBarController.viewControllers objectAtIndex:i] isKindOfClass:[UINavigationController class]])
[[tabBarController.viewControllers objectAtIndex:i] popToRootViewControllerAnimated:NO];
}
}

Have a look at the UITabBar delegate protocole.
Then use tabBarController:didSelectViewController: that way :
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController != tabBarItem1) {
[self.navigationControllerInTab1 popToRootViewControllerAnimated:NO];
}
}
That way, when you leave the first tab item, it will pop the NavigationController to the rootViewController.

Related

resetting to first view controller when tabbaritem is selected

I have a tab bar with 3 items. Each points to a UINavigationController. Each UINavigationController has several viewControllers beneath. I'm wanting to reset back to the first controller in the navigation when any tab bar item is pressed.
I've specified my TabBarController implementation as a delegate
self.delegate = self and my method below (running in my TabBarController implementation works returning UINavigationControllers.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"%#", viewController);
}
With the log file showing e.g.
UINavigationController: 0x8a31a90>2012-12-31 02:16:40.035 Demo[6142:c07]
when I try popToRootViewController or popViewController within this method it doesn't seem to work. I don't get any errors but my viewControllers don't reset. It seems like I've made a really basic error here but I can't tell what.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"%#", viewController);
[self.navigationController popToRootViewControllerAnimated:YES];
[[self navigationController] popViewControllerAnimated:YES];
}
You need to popToRootViewController on the navigationController (viewcontroller) - not the TabViewController (self).
[viewController popToRootViewControllerAnimated:YES];
I'm not sure if this will help. I had a lot of trouble getting this to work and found that I needed to do the following:
In root view controller (first view app comes to), add a delegate in the .h file.
#interface MGMProductsViewController : UIViewController <UITabBarControllerDelegate>
Add the below code to viewDidLoad in root view controller (.m file).
[self.tabBarController setDelegate:self];
Override method in root view controller (.m file) with the below.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
}
}
I can't attribute this to anyone as I couldn't find the appropriate code again. I think I pieced it together from a few places though the '[self.tabBarController setDelegate:self]' looked to be the key to it working for me.
Good luck.

Get index of tabBar touched

How can i get the index of the tabBar when i touch a tab?
I´ve extended my class to < UITabBarController > and added the method
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}
But that method isn´t triggered when i touch a tab item.
What should i do?
UITabBarController has a property selectedIndex that you can use to find the selected tab.
More information about the UITabBarController can be found here
Also be sure to set the delegate property of your UITabBarController, otherwise the delegate messages (such as didSelectViewController:) will not be received.
Did you set the delegate of the UITabBarController to your delegate class when you create it?

When Press Tabbar But it's not going to it"s Rootviewcontroller

When i press certain Tab it's not going to its rootviewcontroller,
When user changes a tab, for the selected tab I want to push it to its top level controller.
I have implement this method but not works,
-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
What's wrong with this?
How can i do that?
Assuming the method gets called (if not you should set the UITabBarController delegate), you are probably receiving the UINavigationController (which is a subclass of UIViewController) as viewController, you can check by logging it:
-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
NSLog(#"didSelect %#", viewController);
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
if that is the case, viewController.navigationController will probably be nil, you should be doing:
-(void)tabBarController:(UITabBarController *) tabBarController didSelectViewController : (UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]])
[(UINavigationController*)viewController popToRootViewControllerAnimated:NO];
}

Return to first view in UITabBarItem

When I receive a Push Notification, I want to jump to the middle tab on the tabbar and reset the tab (it has multiple table views that the user may have navigated into). I am able to jump to the tab using tabBarController.selectedIndex = 1; ... but how do I reset to the first view?
Implement UITabBarControllerDelegate on a controller class (perhaps your app delegate) and implement tabBarController:didSelectViewController: to pop all pushed view controllers off the navigation stack:
- (void) tabBarController:(UITabBarController *) tabBarController didSelectViewController:(UIViewController *) viewController {
if([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
}
}

How to remove a UITabBar badge after the user clicks another tab?

I want to remove a badge as soon as the user clicks another tab. I am trying to do:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem;
tbi.badgeValue = nil;
}
But it doesn't work.
You want to remove a badge from the current tab, or the one touched?
The right place to do this, either way, is in your tab bar controller delegate, in:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
Note that this function gets called whenever the user taps on a tab bar button, regardless of whether the new view controller shown is different from the old one, so you'll want to track your current visible view controller. This is where you'll update that, too:
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
if(viewController != self.currentTabVC) {
// if you want to remove the badge from the current tab
self.currentTabVC.tabBarItem.badgeValue = nil;
// or from the new tab
viewController.tabBarItem.badgeValue = nil;
// update our tab-tracking
self.currentTabVC = viewController;
}
}