Detect selected tabbar when switching tab bar - iphone

I have three tab bar application and my tabs is TAB1, TAB2 , TAB3 i have write the following code in TAB1 view controller to detect which tab user have pressed
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"tab selected: %#", item.title);
}
But this delegate never get called
i have setup my tab in appdelegate.m something like that
- (void)setupTabBar
{
self.myWorkListViewController = [[MyWorkListViewController alloc] initWithNibName:#"MyWorkListViewController"
bundle:nil];
self.askHRViewController = [[AskHRViewController alloc] initWithNibName:#"AskHRViewController"
bundle:nil];
self.moreViewController = [[MoreViewController alloc] initWithNibName:#"MoreViewController"
bundle:nil];
self.bookLeaveViewController = [[BookLeaveViewController alloc] initWithNibName:#"BookLeaveViewController"
bundle:nil];
self.helpViewController = [[HelpViewController alloc] initWithNibName:#"HelpViewController"
bundle:nil];
// Create navigation controllers
workListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.myWorkListViewController];
askHRNavigationController = [[UINavigationController alloc] initWithRootViewController:self.askHRViewController];
bookLeaveViewController = [[UINavigationController alloc] initWithRootViewController:self.bookLeaveViewController];
moreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.moreViewController];
helpNavigationController = [[UINavigationController alloc] initWithRootViewController:self.helpViewController];
[self setTabBarImagesAndText];
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
}

you can detect selected Tabbar Item like this way:-
as par your code you just need to add this line
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
in .h file define like
#interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
{
//declare your Tabbar controller with #proparty
}
in .m file
//#synthesize here your Tabbar controller
- (void)viewDidLoad
{
self.yourTabbarControler.delegate=self;
[super viewDidLoad];
}
and now put this delegate of UITabbarController
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(#"controller class: %#", NSStringFromClass([viewController class]));
NSLog(#"controller title: %#", viewController.title);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}

You have to "wire up" the delegate, either using code (in ViewDidLoad etc.) or in interface builder.
Look at this answer which explains how to connect a textView delegate (it's pretty much the same): https://stackoverflow.com/a/1785366/764575

Related

suffering with UITabbaritems

For some days now I have been suffering with UITabBarItems.
I have done my application in programatical way, not using Interface Builder.
I have both a UINavigationBar and a UITabbarController.
From home page when I have proceed with navigation (I mean when I move to next page) at that time when i have clicked next tab item (Contact), and again when I have clicked the home button, it is not moving to home page, it is remaining with the previous page where I left that one, it is not redirecting to home page at all,
I have placed the UITabbarController-code in my application delegate:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] init];
viewController *vc = [[viewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[navController.tabBarItem setTitle:#"Home"];
[navController.tabBarItem setImage:[UIImage imageNamed:#"home.png"]];
viewController1 *vc1 = [[viewController1 alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:vc1];
[navController1.tabBarItem setTitle:#"Contact"];
[navController1.tabBarItem setImage:[UIImage imageNamed:#"contact.png"]];
viewController2 *vc2 = [[MapViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:vc2];
[navController2.tabBarItem setTitle:#"Info"];
[navController2.tabBarItem setImage:[UIImage imageNamed:#"info.png"]];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, navController1,navController2, nil];
_tabBarController.delegate = self;
[_tabBarController setViewControllers:viewControllers];
[self.window setRootViewController:_tabBarController];
[self.window makeKeyAndVisible];
return YES;
I didn't used these methods:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Please give me suggestions, or something that may be useful to me.
Default behaviour of UITabbarController is, that if you tap a tab and this tab contains a navigationcontroller, you get the page which is on top of the navigationcontrollers viewcontroller-stack.
To avoid this you have to popToRootViewController when you tap on the tab.
If you implement
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
it should do the trick.

how to show a tab bar controller after the login screen is launched?

This is a view based application.
in delegate.m file I have done like this to launch login screen initially:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[window addSubview:viewController.view];
[window makeKeyAndVisible];
LoginView *loginView=[[LoginView alloc]initWithNibName:#"LoginView" bundle:nil];
[window addSubview:loginView.view];
}
By adding the above code I have launched login screen sucessfully, but at the bottom of my login screen I can see a space left out.
How can the tab bar controller get launched after sucessful login?
i have creatd a method called login in my LoginView.m file:
-(void)login
{
if(login)
{
TabBarController *tabBarController = [[TabBarController alloc] initWithNibName:#"TabBarController" bundle:nil];
[self.view addSubView: aTabBarController.view];
}
[aTabBarController release];
Please help me out of this with the appropriate code.
you have to create on method in appDelegate like.. and In appDelegate.h you have to create an object like this
UITabBarController *Obj_tabbar;
and then in .m file,
-(void) switchToTabbarController
{
Obj_tabbar.delegate = self;
Obj_tabbar.selectedIndex = 0;
Tracking_HomeVC *obj = [[Tracking_HomeVC alloc]init];
[self tabBarController:Obj_tabbar didSelectViewController:obj];
[self.window addSubview:Obj_tabbar.view];
}
// At this point Tracking_HomeVC is the first view controller of the TabbarController. and it will be added on window.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([tabBarController selectedIndex] == 0)
{
//Write your code here to do with the first view controller object.
}
}
and then call it from your LoginView like..
-(void)LoginPressed
{
AppAppDelegate *delegate =(AppAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate switchToTabbarController];
}
Your login view (or it's controller if you have one which it looks like you don't) should tell the appDelegate to swap the RootViewController to be a taBarController. You do NOT want the loginview to be trying to add a tabBar as a child of itself.
One way of doing it is creating a tabbarcontroller like normal in your appdelegate and set it as rootviewcontroller:
TOTabBarController *tabBarController = [[TOTabBarController alloc] init];
UIViewController *vc1 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *vc2 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *vc3 = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *vc2_nc = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *vc3_nc = [[UINavigationController alloc] initWithRootViewController:vc3];
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2_nc, vc3_nc, nil];
[tabBarController setViewControllers:viewControllers];
//set tabbarcontroller as rootviewcontroller
[[self window] setRootViewController:tabBarController];
Then display the login screen modally (without animation) if the user is not logged in:
if (not logged in) {
UIViewController *lvc_nc = [[UIViewController alloc] init];
[[[self window] rootViewController] presentModalViewController:lvc_nc animated:NO];
}
Hope that helps!

Navigation Items in UITableViewController are not appearing?

I am displaying a UITableViewController inside of a UITabBarController that is being presented modally:
-(IBAction)arButtonClicked:(id)sender{
//this is a uitableviewcontroller
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
arTabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arViewController, lbViewController, nil];
arTabBarController.selectedViewController = arViewController;
[self presentModalViewController:arTabBarController animated:YES];
}
In my viewDidLoad for arViewController method I am setting the navigation items:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;
self.title = #"AR";
leaderBoardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(leaderBoardButtonClicked:)];
self.navigationItem.rightBarButtonItem = leaderBoardButton;
}
My navigation bar doesn't appear when it is inside of the UITabBarController, but when I push the view itself I am able to see it.
What am I missing?
Heh, I've been stumped by this too. What you need to do is send the rootViewController.
I've never used a tabBar for anything except on the main screen but ur code will probably look like this:
after arTabBarController.selectedViewController = arViewController;
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController: arTabBarController] autorelease];
[self presentModalViewController: navController animated:YES];
Like I said I haven't done it with a tabBar but I'm pretty sure it will be something along these lines
I needed to add a UINavigationBar:
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[self presentModalViewController:arTabBarController animated:YES];
There is a simple solution, put setting in view will appear
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
hope it help some newbies;

problem in UITable cell selection

hai ,
i have coded in UITableview in the method as follows.but when i touch the cell or row ,it wont
go to the next page(navigation did not work).have i to declare navigation conroller in other file.but i have coded app delegate in applicationdidfinishmethod for tab bar through dynamic.how can i link navigation?
the code:
UITableview;(TableViewController)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubController *nextController = [[SubController alloc] init];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
appdelegation:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
tabBarController.navigationItem.title = #" News";
TableViewController *rtbfViewController = [[TableViewController alloc]
init];
rtbfViewController.tabBarItem.title = #"News";
InfoViewController *infoViewController = [[InfoViewController alloc]
initWithStyle:UITableViewStyleGrouped];
infoViewController.tabBarItem.title = #"Info";
tabBarController.viewControllers = [NSArray
arrayWithObjects:rtbfViewController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
The problem is, that you don't have a UINavigationController, so self.navigationController in your TableViewController is nil (and thus messages sent to this property are ignored). You should modify your code in the app delegate as follows:
// [...] create tab bar view controller...
// create navigation controller with TableViewController instance as root view controller
TableViewController *rtbfViewController = [[TableViewController alloc] init];
rtbfViewController.tabBarItem.title = #"News";
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rtbfViewController];
// [...] create other view controllers
// NOTE: add the navigation controller to the tab bar controller, rather than the TableViewController
tabBarController.viewControllers = [NSArray arrayWithObjects:navController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
And don't forget to release your view controllers afterwards:
[rtbfViewController release];
[navController release];
[infoViewController release];

Implement Back action into UITabBarController

I have a controllerView (MenuControllerView) with a button inside, when I click on the button a new ViewController will appear with a TabBarController created programmatically like this:
UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
viewController4 = [[ViewController4 alloc] init];
tabBarController,viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 ,viewController4, nil];
[[self tabBarController] setSelectedIndex:1];
[topView addSubView:[tabBarController view]];
Instead of displaying ViewController1 for the first button Item, I want to put an action Back in it to return to my MenuViewController, but I don't know how how to do it.
Thanks
Have you considered presenting the UITabBarController as a modal view controller and implementing UITabBarControllerDelegate? e.g. this seems to work for me (I make the third tab return to MenuViewController here):
#interface MenuViewController : UIViewController <UITabBarControllerDelegate>
...
- (IBAction) onButtonPressed:(id)sender
{
UITabBarController* tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 , nil];
[[self tabBarController] setSelectedIndex:1];
tabBarController.delegate = self;
[self presentModalViewController:tabBarController animated:NO];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if (viewController == viewController3)
{
[self dismissModalViewControllerAnimated:NO];
return NO;
}
return YES;
}
I doubt that this approach is a good one. You'll gonna break typical iPhone behaviour which will confuse users. The TabBarController is designed (functionally and technically) to change between views while a NavigationController is for pushing and popping views (go forth and back). Of course you can combine those (which is not always easy), but you shouldn't use TabBar as NavigationBar.
if I understand right, you can just remove your tabbar's view from superview. smth like
[[tabBarController view] removeFromSuperview];
if you just want to handle selection of tabbar item, you can use tabBar:didSelectItem: method of th UITabBarDelegate protocol.
Is this what you're trying to do?
This automatically created with a UINavigationController upon pushing to a child view controller.
[self.navigationController pushViewController:yourChildViewController animated:YES];