How to get tabbar click event without tabbarcontroller in iphone? - iphone

I am trying to implement tabbar in my 2nd view.
I am able to place tabbar with 5 items on it. I know to handle those click events , i have to use tabbarcontroller.
My question is , taking tabbar on view,
How to call each item selected method without tabbarcontroller?
(My assumption is that tabbar is an object like button , and we can write a click method for that programmatically. So without Tab Controller also, we can access selected item method )
Is there any way to do that?

If you don’t want to use tabbarcontroller, then its always better to use Segmented Control.
It has similar kind of operations & its also simple to use. Use need to just create multiple uiviews on view & just play Hide-n-seek with them. If you want I have some code. Will paste here, if you want.

I think, you can do it like this. (*** Note : Its not tested code)
Add <UITabBarDelegate> in .h file
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item == firstItem)
NSLog(#"Did Select Here”);
else if(item == firstItem)
NSLog(#"Did Select Here”);
}

Create UITabbar and define UITabbarDelegate in .h file and write the following code in respective view:
- (void)viewDidLoad {
[super viewDidLoad];
UITabBarItem * newItem1 = [[UITabBarItem alloc] initWithTitle:#"First" image:[UIImage imageNamed:#"setting.png"] tag:1];
UITabBarItem * newItem2 = [[UITabBarItem alloc] initWithTitle:#"Second" image:[UIImage imageNamed:#"setting.png"] tag:2];
UITabBarItem * newItem3 = [[UITabBarItem alloc] initWithTitle:#"Third" image:[UIImage imageNamed:#"setting.png"] tag:3];
[tabbar setItems:[NSArray arrayWithObjects:newItem1,newItem2,newItem3,nil]];
tabbar.delegate = self;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(#"Tabbar selected itm %d",item.tag);
switch (item.tag) {
case 1:
//first selected
break;
case 2:
//second selected
break;
case 3:
//third selected
break;
default:
break;
}
}

Related

Items of a UITabBar are not being highlighted when selected

I'm having a trouble with a UITabBar with UITabBarItem added programmatically, the selected item is not being highlighted in white as usual.
Here's the source
UIImage *imageX = [UIImage imageNamed:#"sample.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:#"Sample" image:imageX tag:1];
NSArray *array = [NSArray arrayWithObjects:tabBarItem, nil];
[tabBar setItems:array];
When I select the item, the method didSelectItem of the delegate is called and the action is executed, but the image is not being highlighted by the bar...
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
Anyone know what's wrong ? I have to call other method to pass the highlighted image or something like that ?
The items of the tab bar are added programmatically but the tab bar I add via Storyboard.
Also, I did a test where the tab bar and the tab bar items are added via Storyboard, and everything works, including the highlighted icon is shown when it's selected
Thanks !
I solve the problem by adding code in - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; function :
self.tabBar.selectedItem = item;
I don't know why I have to do this, but it's working.

Adding subViews to a viewcontroller

I am having an issue with trying to load a viewcontroller onto another viewcontroller as a subview.
what I have is a NavigationController that loads some viewControllers in as views (pop and push etc) that works perfectly. then I have decided to put a tabBar into a viewController which then looks after all of the selection stuff using a switch statement, this switch statement then calls a method inside one of the viewControllers that appears inside the navigationController.
The method inside this viewController then trys to set another viewcontroller as a subview to the viewcontroller thats inside the navgiation controller.
this is my code.
TabBarViewController.m
#import "DetailViewController.h"
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
switch (item.tag) {
case 0:
{
NSLog(#"item 1 selected");
DetailViewController *dVC = [[DetailViewController alloc] init];
[dVC tabBarSelectedAction];
}
break;
default:
break;
}
}
so this catches the selected item on the tab bar... then fires off a msg to the DetailViewController method to load the new subview onto DetailViewController.view
- (void)tabBarSelectedAction
{
ButtonOneViewController *b1VC = [[ButtonOneViewController alloc] initWithNibName:#"ButtonOneViewController" bundle:[NSBundle mainBundle]];
[self.testView addSubview:b1VC.view];
}
and this is where I am trying to load the subview onto the screen.. I think I am doing it right but for some reason its not displaying.. another thing I would like to do is animate this view from the bottom of the screen up..
any help would be hugely appreciated.
When you created your new DetailViewController you didn't make it part of the view hierarchy through a push or present type of method. Adding a subview may or may not be working but you won't see it because the object you're adding it to isn't using the screen.
Your method should probably look like this. Assuming self DetailViewController.
- (void)tabBarSelectedAction {
ButtonOneViewController *b1VC = [[ButtonOneViewController alloc] initWithNibName:#"ButtonOneViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:b1VC animated:YES];
}
Even with that, I think your logic is a little screwed up. You allocate and initialize DetailViewController but you never present it anywhere. So how are you expecting to see a modal view in DetailViewController, if you never present it.
EDIT: Taking into consideration your comment of adding it to the UINavigationController, you would change it to look something like this..
[[self navigationController] presentModalViewController:b1VC animated:YES];
EDIT2: Also, you're initializing a class, just to call a method which is already self. Your -didSelectItem: method should look more like this.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 0:
{
NSLog(#"item 1 selected");
[self tabBarSelectedAction];
}
break;
default:
break;
}
}

programmatically create uitableview

Hi there I am currently testing how to develop an application that has several tableviews with their own parent/child structures that are accessed from a main menu.
I would like to know how to generate the new tableview with the uinavigationcontroller menu but a shared uitabbar as this is the first time I have tried anything like this normally I just stick with apples templates.
Here is a general acitecture of what I am wanting to achieve, any comments suggestions code example would be greatly appreciated and from there I can work through it myself, its more of a question as to where the heck do I start :)
So far I have the main window set up with a UIAction catching the different button clicks, I need to figure out how to allow all children to share the specific UITabbar and then how to set up so that individual branches have their own UINavigationController menu if needed.
this is my UIAction
//Delegate.m
//--- Caputer button clicks ---
- (IBAction)buttonClick: (UIButton *) sender
{
if ([sender isEqual:orangeButton]) {
NSLog(#"orangeButton Pressed");
}
else if ([sender isEqual:blueButton]) {
NSLog(#"blueButton Pressed");
}
else if ([sender isEqual:greenButton]) {
NSLog(#"greenButton Pressed");
}
else if ([sender isEqual:purpuleButton]) {
NSLog(#"purpleButton Pressed");
}
}
If you created a "TabBar based application", then adding the navigationController for the desired tab is pretty easy. Drag the "UINavigationController" inside the "Tab Bar Controller" in the Main Window:
As to "How to generate tableViews", the simplest way, is to create a generic TableView, call it LevelTableView.h/.m file (and it could have its own .xib), where you can add whatever you wish, and then, keep creating new ViewControllers, for instance, in a level of this tableView:
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
LevelTableView *anotherLevel = [[LevelTableView alloc] initWith...];
[self.navigationController pushViewController:anotherLevel animated:YES];
[anotherLevel release];
}
The point is, that this "LevelTableView" is created once, but instantiated various time for each level you want to add, the content is the only thing that changes.
I have exactly something like that. What I do is that I created a different class which I call "TabBarController" and where I initialize all the different views that are going to my tabs:
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
myTabBarController = [[UITabBarController alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
Then when the user clicks on one of the buttons in the main view this is what I do:
- (IBAction)yourAction:(id)sender {
TabBarController *tabBarController1 = [[TabBarController alloc] init];
tabController.selectedIndex = 1; ==> chose which tab you want to show
[[[UIApplication sharedApplication]delegate].window setRootViewController:tabBarController1.myTabBarController];
}
Does the job pretty nicely.
Add the Navigation Controllers into the tabbar controller. Then Hide the NavigationController and according to your schema use toolbars to pop and navigate your views.

iPhone dev - UIViewController title, tabBarItem, tag

I have a UITabBarController that manages 5 View Controllers. I create their tab bar items in their "init" methods so that they will be displayed before the view is loaded. I'm just wondering what way I should do it, because there seems to be so many ways. For example, for my DatePickerViewController:
- (id)init {
if((self = [super init])) {
// ================ THIS ==========================
UIImage *clockIcon = [UIImage imageNamed:#"clockicon.png"];
UITabBarItem *localTabBarItem = [[UITabBarItem alloc]
initWithTitle:#"Date" image:clockIcon tag:0];
[self setTabBarItem:localTabBarItem];
[localTabBarItem release];
// ================ OR THIS ========================
[self setTitle:#"Date"];
UITabBarItem *localTabBarItem = [[UITabBarItem alloc] init];
[localTabBarItem setImage:[UIImage imageNamed:#"clockicon.png"]];
[self setTabBarItem:localTabBarItem];
[localTabBarItem release];
// ================ OR THIS ========================
UITabBarItem *localTabBarItem = [[UITabBarItem alloc] init];
[localTabBarItem setTitle:#"Date"];
[localTabBarItem setImage:[UIImage imageNamed:#"clockicon.png"]];
[self setTabBarItem:localTabBarItem];
[localTabBarItem release];
}
return self;
}
Which way should I do it? And why is there a title for both the tabBarItem and the View Controller? And I don't think I need the tag (which is set in the first method).
Thanks!!
Well in my opinion any of these ways are ok, it might be more readible when you declare the UIImage in one line and set it in a different line rather than doing it all inline, but at the end you get the same result.
The TabBarItems have a title which is the text that will show in the tab bar item iteself. View Controllers have a title for Navigation Controller purposes, the View Controllers title is displayed in the NavigationControllers NavBar when set. And you do need tags, tags is the way you tell the buttons apart when someone click on them (when u manage the TabBar on your own).
The reason there are several ways to set the title is for convienece. You may want to display one title in the navigation bar and one title in the tab bar.
This actually quite common since there is less space to display text in the tab bar.
Like many things in Cocoa, there is more than one way to do it. The only "correctness" you need to be concerned about is what works best for your situation.

Programmatically pressing a UITabBar button in Xcode

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.
What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).
Any help would be greatly appreciated.
Dave
[myTabBarController setSelectedIndex:index]
EDIT: Answering the part 2 question from the comment:
You can define a method in AppDelegate for switching to a different tab.
And you can get hold of appdelegate from anywhere and send a message..
something like:
MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
[appDelegate SwitchToTab:index]
alternatively...
[self.parentViewController.tabBarController setSelectedIndex:3];
I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.
Anyhow, I hope this helps someone:
I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:
AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];
When I should have been doing this:
AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];
Newbie mistake.
For this, You just need to take UITabBar controller -
.h File -
UITabBarController *_pTabBarController;
#property (nonatomic, retain) IBOutlet UITabBarController *_pTabBarController;
.m File -
// synthesize it
#synthesize _pTabBarController;
At initial load
// You can write one function to add tabBar -
// As you have already mentioned you have created an array , if not
_pTabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
UINavigationController *theNavigationController;
_pController = [[Controller alloc] initStart];
_pController.tabBarItem.tag = 1;
_pController.title = #"Baranches";
theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
theNavigationController.tabBarItem.tag = 1;
theNavigationController.tabBarItem.image = [UIImage imageNamed:#"icon_branches.png"];
[localViewControllersArray addObject:theNavigationController];
[theNavigationController release];
than you can set index as per your needs
self._pTabBarController.selectedIndex = 0; // as per your requirements
[self.parentViewController.tabBarController setSelectedIndex:3];
Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.
Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:
UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];
Thank you for this post/answers it helped out a lot in my project.
I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.
Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:
FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview]
[self.view insertSubview:firstVC.view belowSubview:self.tabBar];
[self.tabBar setSelectedItem:self.firstTabBarItem];
self.selectedViewController = firstVC;
You might have similar code already inside your didSelectedItem..
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if (item == self.firstTabBarItem)
// Right here
}
else if ...
}