Changing UIViewController in UITabBarController on clicking the tab - iphone

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

Related

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

Get NavigationController Stack of tabbarItem in a tabbarController

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

iphone app - uitabbarcontroller to always select first view in a uinavigationcontroller?

I am building an iphone application.
I have a tabbarcontroller that has 2 tab items. Each tabitem links to a different navigationcontroller. Each navigationcontroller links to a hierarchy of tableviewcontrollers.
When a user clicks on tab 1, then clicks on an item in the table, then clicks tab 2, and then clicks on tab1, the application shows the table that he was just looking at before he clicked on tab2.
How do i get the app to show the first table of tab 1 every time he clicks on tab 1 instead of showing the most recent table he was looking at before leaving tab1?
I would prefer a programmatic solution as opposed to using xcode storyboard. But if none exists, then storyboard solution is fine too.
Call popToRootViewControllerAnimated: on the NavigationController when the TabBarController changes tab that is being displayed.
Try this basic sample to create a UItabBar and UInavigationController for each UItabBarItem from scratch :
in your header file (appdelegate.h) , add this delegate :
#interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
in the function called "didFinishLaunchingWithOptions" , add this part of code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navController=[[UINavigationController alloc] init];
m_ViewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
[navController pushViewController:m_ViewController1 animated:NO];
UINavigationController *navController2=[[UINavigationController alloc] init];
m_ViewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
[navController pushViewController:m_ViewController2 animated:NO];
UITabBarController *mtabBarController = [[UITabBarController alloc] init];
mtabBarController.view.frame = CGRectMake(0, 0, 320, 460);
// Set each tab to show an appropriate view controller
[mtabBarController setViewControllers: [NSArray arrayWithObjects:navController1,navController1,navController2, nil]];
self.window.rootViewController = mtabBarController;
mtabBarController.delegate = self;
[self.window makeKeyAndVisible];
return YES;
}
Then in this function , don't forget to add the popToRootViewControllerAnimated function :
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
[m_ViewController1.navigationController popToRootViewControllerAnimated:YES];
[m_ViewController2.navigationController popToRootViewControllerAnimated:YES];
return YES;
}
in my appdelegate.h file, I changed the line
#interface wscAppDelegate : UIResponder <UIApplicationDelegate>
to
#interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
Then in my CustomTabBarController in the viewDidLoad function i added these lines:
wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;
Then in appdelegate.m file, I added this function
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
for(int c=0; c<[tabBarController.viewControllers count]; c++)
{
UINavigationController * navcontroller = [tabBarController.viewControllers objectAtIndex:c];
[navcontroller popToRootViewControllerAnimated:YES];
}
return YES;
}

popToRootViewController crashes when called by UITabBarControllerDelegate

I have a UITabBarController with 4 UINavigationControllers. I have implemented the didSelectViewController Delegate Method as follows:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
It crashes when a NavigationController is at a 2nd Level after didSelectRowAtIndexPath pushes a new viewController onto the stack.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
// ...
detailViewController.title = [self.temp objectAtIndex:indexPath.row];
detailViewController.sort = self.title;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
Of course the debugger with NSZombies enabled doesn't give any feedback.
However, if I add retain to detailViewController alloc;
RootViewController *detailViewController = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] retain];
It works, but leaks memory.
Any ideas what is wrong, how to fix, what is happening?
I have the similar scenario and i came up with following solution.
In my application i have login screen at launch and then I have UITabbarController with 4 UINavigationControllers.
I have created property of UINavigationController in AppDelegate.h file.
#property (strong, nonatomic) UINavigationController *navigationController;
Then
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
{
//Override point for customization after application launch.
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Now when you need to pop to RootViewController then use following code
#import "AppDelegate.h"
[((AppDelegate *)[[UIApplication sharedApplication] delegate]).navigationController popToRootViewControllerAnimated:YES];
Hope this solves your problem.