Get NavigationController Stack of tabbarItem in a tabbarController - iphone

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);
}

Related

Calling poptoviewcontroller function from a viewcontroller

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.

UITabbarController + UINavigationController creating issue for push and pop

In my iPhone application I am using UITabbarController + UINavigationController, created in appDelegate.
My issue is as follows:
On screen- I have tableview. When one selects cell I am pushing the controller to screen-B. Now without poping it if I select another tab from tabbar, then view gets refreshed but navigationbar is not getting refreshed. It displays navigationbar of screen-Bscreen-B.
Used below code but nothing seems to solve my issue:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]])
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
}
try this in appdelegate
UINavigationController *screen1=[[UINavigationController alloc] initWithRootViewController:controller1];
UINavigationController *Bscreen=[[UINavigationController alloc] initWithRootViewController:controller2];
NSArray *tabbararray=[[NSArray alloc] initWithObjects: screen1,Bscreen,nil];
self.TabControllerObj.viewControllers=allviewcontrollers;
and make tabbar as root viewcontroller of your window

Tab bar method?

I added this tab bar in view controller 1 and it works perfectly, the only issue is I want it to do the following function [self.navigationController popViewControllerAnimated:YES];, but in view controller2 when the tab bar button is touched. What would be the best course of action?
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
homeNavigationController.tabBarItem = [[DSTabBarItem alloc] initWithFinishedSelectedImage:[UIImage imageNamed:#"home"]
finishedUnselectedImage:[UIImage imageNamed:#"home1"]
iconSize:CGSizeMake(76, 59)
tag:0];
[tabBarViewControllers addObject:homeNavigationController];
If you added tabbarcontroller programmatically . And In that class add the following method.
Then you navigation controller will changed to root view. And you can place your own between the if to your requirement.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController*)self.tabBarController.selectedViewController popToRootViewControllerAnimated:YES];
}
}
DetailSettingsViewController *settings = [[DetailSettingsViewController alloc] initWithNibName:#"DetailSettingsViewController" bundle:nil];
[self.navigationController pushViewController:settings animated:YES];

Changing UIViewController in UITabBarController on clicking the tab

I have UITabBarController with 4 tabs. I have individual 4 views on each of the tab of the UITabBarController. I want to change a UIViewController of second tab of the UITabBarController whenever i click the third tab of the UITabBarController.
You can do this by using UITabBarController's delegate as -
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([tabBarController selectedIndex] == 2)
{
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[tabBarController viewControllers]];
NewViewController *nvc = [[NewViewController alloc] init];
[arr replaceObjectAtIndex:1 withObject:nvc];
[nvc release];
[tabBarController setViewControllers:arr];
}
}

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];
}