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];
}
Related
Let me explain clearly.
I have a tabbarcontoller in the viewcontroller which is the main view controller of the single view application project.
I added a tabbarcontroller to the viewcontroller as the subview. In the tabbarcontroller, I added two navigation controllers like below image,
I have added three(named First, Second, Third) more viewcontrollers as new file.
If I navigate from one viewcontroller to other in a tab using below,
third = [[Third alloc] initWithNibName:#"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];
If I switch to next tab and come back to the previous tab, it should popto the previous view controller, how to do this?
I tried
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
not succeed,
I also tried,
[third popto];
In the third viewcontroller,
-(void)popto
{
[self.navigationController popViewControllerAnimated:YES];
}
nothing happened.
Now, I have to click the tab again to poptoviewcontroller to the first viewcontroller.
Any ideas will be highly appreciated.
You should try using
[viewController.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
instead of
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
Try the below code snippet
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)viewController;
if (navController && ([[navController viewControllers] count] > 0))
{
[navController popToRootViewControllerAnimated:NO];
}
return YES;
}
Hope it may work for you.
How about overriding UITabbarController for a custom one and implement the following method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
Next loop trough all the 'viewcontrollers' of the tabbar (in this case the navigation controllers) and call on all the navigation controllers popToRootViewController.
This might work.
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.
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.
I want to get the list of UIViewControllers i.e UINavigationController stack at particular index of tabbaritem in UITabBarController.
Please ellaborate ..!
Please guys join hands
Do this:
NSArray *arrControllers = self.tabBarController.viewControllers;
for(UIViewController *viewController in arrControllers)
{
if([viewController isKIndOfClass:[UINavigationController class]])
{
//NavigationController
UINavigationController *navCtrl = (UINavigationController *)viewController;
NSLog(#"%#",navCtrl.viewControllers);
}
else
{
// view controller
}
}
Implement the callback of UITabBarControllerDelegate if you want to check this when a tab is changed:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//if you're using navigationController
UINavigationController *navC=(UINavigationController *)viewController;
NSArray *arrayVc=navC.viewControllers;
NSLog(#"%#",arrayVc);
}
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];
}
}