This is my code for my tab bar
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(#"Crops ", #"Articles");
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 376, 320, 44)];
item1 = [[UITabBarItem alloc] initWithTitle:#"TypesToTry" image:[UIImage imageNamed:#"crops.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"WhenToPlant" image:[UIImage imageNamed:#"crops.png"] tag:1];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4,item5, nil];
[tabBar setItems:items animated:YES];
[tabBar setSelectedItem:nil];
tabBar.delegate=self;
[self.view addSubview:tabBar];
}
what i want is whenever i tap on the 1st tab i want to go to that particular description ,so where to right this action.Please help me in this.
The questions may be simple,but for me they are difficult :),Please help this new developer
Thanks
Hi i think you can add following line of code to make your tab bar item workable
[item1 performSelectorOnMainThread:#selector(Yourfunction) withObject:nil waitUntilDone:NO];
[item2 performSelectorOnMainThread:#selector(Yourfunction) withObject:nil waitUntilDone:NO];
put this line at end of -(void) didLoad, i hope it will work for you
Related
I'm developing an iPhone app where I want to place tab bar on top.
Here is my code
tabBarController = [[UITabBarController alloc]init];
tabBarController.view.frame= CGRectMake(0, 0, 320, 40);
NSArray *arr = [[NSArray alloc]initWithObjects:[[DemoView alloc] init], [[DemoView alloc] init], nil];
tabBarController.viewControllers = arr;
[tabBarController.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:tabBarController.tabBar];
But I cant see tab bar. I don't what the problem is.
Thanks
you dont have to take another view to add tabBar. You can directly add to your window
This code worked for me:
UITabBarController *tabC = [[UITabBarController alloc]init];
tabC.tabBar.frame = CGRectMake(0, 0, 320, 70);
NSArray *arr = [[NSArray alloc]initWithObjects:firstObj,secObj, nil];
tabC.viewControllers = arr;
[self.window addSubview:tabC.view];
I'm developing a iphone app that has a main viewcontroller(UITabBarController) with UINavigationControllers in each tab. The problem is that I can't change the TabBarItem's data(title and image).
This is how I load the viewcontrollers:
AroundViewController *aroundViewController = [[AroundViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aroundViewController];
[[navController navigationBar] setTintColor:[UIColor grayColor]];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];
[self presentViewController:tabBarController animated:YES completion:nil];
I'm trying set the tabbaritem in the aroundviewcontroller's init, but I don't succeed.
I was reading and one solution was, in the init,
[self setTitle:#"whatever"];
This worked but I couldn't change the image.
Thanks in advance.
Use the tabBarItem instance of your view controller to access the tab bar item directly!
[self.tabBarItem ....]
[self.tabBarItem setImage: ...]
[self.tabBarItem setTitle: ...]
EDIT:
hm, try the following then in your init or viewWillAppear method:
UITabBarItem* tabBarItem = [[UITabBarItem alloc] initWithTitle:#"YourTitle" image:[UIImage imageNamed:#"YourImage.png"] tag:9];
self.tabBarItem = tabBarItem;
[tabBarItem release];
Before setting the UIViewControllers for the UITabBarController you can do this:
[navController.tabBarItem setImage:[UIImage imageNamed:#"MY-IMAGE"]];
[navController setTitle:#"MY-TITLE"];
NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];
I found a solution.
In the viewWillAppear I put:
[[[self navigationController] tabBarItem] setTitle:#"HELLO"];
That works.
I am working on an iPhone app, using UITabBar to develop it. Only the second tabbar title visible in tabbar, but first tabbar title not show in tabbar, how to fix this? please help me,
Thanks in Advance.
below is the source code for your reference.
NSMutableArray *items = [[NSMutableArray alloc]init];
tab =[[UITabBar alloc]initWithFrame:CGRectMake(0, 300, 320, 44)];
[self.tabBarItem initWithTitle:#"sample1" image:nil tag:111];
[items addObject:self.tabBarItem];
[self.tabBarItem initWithTitle:#"sample2" image:nil tag:101];
[items addObject:self.tabBarItem];
[tab setItems:items animated:YES];
[self.view addSubview:tab];
Dont navigate the view controllers, because then your controller which contains the TabBar will get popped out.
When we use UITabBar we dont use navigation in this way.
Just addSubView the new controllers with such frame so that your TabBar will alwaz be visible.
You have to add that tabbarItem in UITabBar.
NSArray *items = [[NSArray alloc]init];
[items addObject:self.tabBarItem]; //add all your tabBarItems in an array
[tab setItems:items animated:YES]; //then set all your tabBarItems in UITabBar like this
Actually you are using same instance of tabBarItem thats'y you are getting only second button.
Do this way, and Please if this helps you then accept the answer.
UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 725, 768, 49)];
NSMutableArray *items = [[NSMutableArray alloc]init];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:#"first" image:nil tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:#"second" image:nil tag:2];
UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:#"third" image:nil tag:3];
UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:#"fourth" image:nil tag:4];
[items addObject:item1];
[items addObject:item2];
[items addObject:item3];
[items addObject:item4];
[tabBar setItems:items];
[self.view addSubview:tabBar];
That's not the way you do to create and add a TabBar programmatically.
You'd rather use this other method: https://stackoverflow.com/a/3844365/655221
Good luck.
i have a view controller with some custom buttons in it, AND AFTER CLICKING a button i am presenting different view .and to one of the button click i am presenting a tab bar controller with navigation controller .
and the code is as follows
TabBarView *tabbar=[[TabBarView alloc]init];
UINavigationController *navNextController = [[UINavigationController alloc] initWithRootViewController:tabbar];
[self presentModalViewController:navNextController animated:YES];
and in tab bar the code is as follows
UINavigationController *localNavigationController;
tabbarcontroller = [[UITabBarController alloc] init];
localControllersArray = [[NSMutableArray alloc] initWithCapacity:3];
tabbarcontroller.delegate=self;
ShoppingListView *shop;
shop = [[ShoppingListView alloc] init];
shop.parentVC=self;
localNavigationController = [[UINavigationController alloc] initWithRootViewController:shop];
[localNavigationController.tabBarItem setTitle:#"Lists"];
[localNavigationController.tabBarItem setImage:[UIImage imageWithContentsOfFile:[IMBundle fullBundlePath:#"Images.bundle/Images/EnhancedMobileLists/list.png"]]];
[localNavigationController viewWillAppear:YES];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[shop release];
FavoritesViewController *fav;
fav = [[FavoritesViewController alloc] init];
fav.parentVC=self;
localNavigationController = [[UINavigationController alloc] initWithRootViewController:fav];
[localNavigationController.tabBarItem setTitle:#"Favorites" ];
[localNavigationController.tabBarItem setImage:[UIImage imageWithContentsOfFile:[IMBundle fullBundlePath:#"Images.bundle/Images/EnhancedMobileLists/favorites.png"]]];
[localNavigationController viewWillAppear:YES];
[localNavigationController viewWillDisappear:YES];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[fav release];
ShareListViewController *share;
share = [[ShareListViewController alloc] init];
share.parentVC=self;
localNavigationController = [[UINavigationController alloc] initWithRootViewController:share];
[localNavigationController.tabBarItem setTitle:#"Share" ];
[localNavigationController.tabBarItem setImage:[UIImage imageWithContentsOfFile:[IMBundle fullBundlePath:#"Images.bundle/Images/EnhancedMobileLists/share.png"]]];
[localNavigationController viewWillAppear:YES];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[share release];
tabbarcontroller.viewControllers = localControllersArray;
[self.view addSubview:tabbarcontroller.view];
the main problem is while clicking the button to present the tab bar view it takes time to load ie for a moment it appears as if the view is hanged and i am presenting a navigation view in another button and it is working fine with out any delay . please tell me whats wrong with my code and how to rectify the delay to present a tab bar while clicking a uibutton in iphone.
thanks in advance
Below code is written into the Appdelegate file and then this method is call on the button when tap on the button this method is call.
-(void)ShowTabAbout{ [viewController.view removeFromSuperview]; self.imgV.frame=CGRectMake(0, 425, 320, 55);
self.imgV.image=[UIImage imageNamed:#"tBar5.png"]; [self.tabctr.view
addSubview:self.imgV]; self.tabctr.selectedIndex=4; // [self
animationTabCode]; [window addSubview:tabctr.view];
}
Following code for the Animation for navigate the view controller.
-(void)animationTabBarCode{
tr.type=kCATransitionPush;
tr.subtype=kCATransitionFromRight;
tr.delegate=self;
[self.window.layer addAnimation:tr forKey:nil]; }
following code is implanted into the button which located view controller method file.
-(IBAction)btnTapped:(id)sender{
[appDelegate ShowTabAbout];
[appDelegate animationTabBarCode];
}
I currently have my navigation controllers defined in my appDelegate as followed (code summarized):
- (void) applicationDidFinishLaunching {
tabBarController = [[UITabBarController alloc] init];
FlagList *flagList = [[FlagList alloc] initWithApiCall:API_PUBLICTIMELINE andTitle:#"Home"];
UITabBarItem *homeTab = [[UITabBarItem alloc] initWithTitle:#"Home"
image:[UIImage imageNamed:#"home.png"]
tag:0];
flagList.tabBarItem=homeTab;
[homeTab release];
tabBarController.viewControllers=[NSArray arrayWithObjects:flagList,nil];
[flagList release];
[rootViewController release];
rootViewController = [[UINavigationController alloc] initWithRootViewController:[tabBarController autorelease]];
rootViewController.navigationBar.barStyle=UIBarStyleDefault;
}
I want to set a title in the navigationBar of my FlagListView. HOWEVER, I want to be able to do this in the -viewDidLoad method of my FlagList UITableViewController class. How can I access this property?
I tried:
[[self navigationItem] setTitle:#"Home"];
..but it doesn't seem to work. Can someone please tell me what I'm doing wrong?
Assuming FlagList is a descendant if ViewController use [self setTitle:#"Home"] instead of [[self navigationItem] setTitle:#"Home"];
[self setTitle:#"Home"];
This should work.