For an iPhone app how can I create a tab view programmatically, preferably in Objective-C?
It's quite simple to create a UITabBar via the UITabBarController. The following example should work within your AppDelegate class.
App Delegate Interface
Firstly, within the interface, we'll define our UITabBarController.
UITabBarController *tabBarController;
App Delegate Implementation
Then, within the implementation file's application:didFinishLaunchingWithOptions: method, we'll then initialise our tab bar controller.
// Initialise our tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
Next, you need to create the view controllers that you want to add to the tab bar controller. We'll need to add some information into these to set the tab's title/icon, but I'll come back to that at the end.
// Create your various view controllers
UIViewController *testVC = [[TestViewController alloc] init];
UIViewController *otherVC = [[OtherViewController alloc] init];
UIViewController *configVC = [[ConfigViewController alloc] init];
As the setViewControllers:animated: method requires an array of view controllers, we'll add our view controllers to an array and then release them. (As the NSarray will retain them.)
// Put them in an array
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil];
[testVC release];
[otherVC release];
[configVC release];
Then simply provide the UITabBarController with the array of view controllers and add it to our window.
// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers animated:NO];
// Put the tabBarController's view on the window.
[window addSubview:[tabBarController view]];
Finally, make sure you call [tabBarController release]; within your dealloc method.
View Controller Implementation
Inside each of your view controllers, you'll also want to set the title and icon for the tab within the init method as follows:
// Create our tab bar item
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:#"YOUR_IMAGE_NAME.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:#"YOUR TITLE"];
This is how we have to create tabbar programmatically
UINavigationController *BandNavigationController3;
AudienceSettingsViewController *audienceSettingsViewView =[[AudienceSettingsViewController alloc]initWithNibName:#"AudienceSettingsViewController" bundle:nil];
BandNavigationController3 = [[UINavigationController alloc]initWithRootViewController:audienceSettingsViewView];
BandNavigationController3.tabBarItem.title = #"Settings";
BandNavigationController3.tabBarItem.image = [UIImage imageNamed:#"settings.png"];
[BandNavigationController3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:4];
BandNavigationController3.navigationBar.hidden = YES;
[bandTabBarArray addObject:BandNavigationController3];
[BandNavigationController3 release];
[audienceSettingsViewView release];
[tabBarController setViewControllers:bandTabBarArray];
[bandTabBarArray release];
Related
I'm currently attempting to use the UITabBar for an iOS app that contains 7 tabBar Items.
When I use the storyboard, I am able to achieve all 7 tabBarItems.
When I programmatically add the tabBarItems, It forces a "More" Button to access the other tabBarItems.
Is there a way programmatically keep all the 7 tabBarItems as when I am manually create the UITabBar?
The Code that I'm using to build the uitabbar in my appdelegate.m
self.tabBarController = [[UITabBarController alloc] init];
FirstViewController *firstVC = [[FirstViewController alloc] init];
SecondViewController *secondVC = [[SecondViewController alloc] init];
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
FourthViewController *fourthVC = [[FourthViewController alloc] init];
FifthViewController *fifthVC = [[FifthViewController alloc] init];
SixthViewController *sixthVC = [[SixthViewController alloc] init];
SeventhViewController *seventhVC = [[SeventhViewController alloc] init];
NSArray *controllers = #[firstVC, secondVC, thirdVC, fourthVC, fifthVC, sixthVC, seventhVC];
self.tabBarController.viewControllers = controllers;
self.window.rootViewController = self.tabBarController;
[_window addSubview:_tabBarController.view];
By design you are limited to 5, from apple docs
If you add more than five items to the viewControllers property, the
tab bar controller automatically inserts a special view controller
(called the More view controller) to handle the display of the
additional items. The More view controller provides a custom interface
that lists the additional view controllers in a table, which can
expand to accommodate any number of view controllers. The More view
controller cannot be customized or selected and does not appear in any
of the view controller lists managed by the tab bar controller. It
appears automatically when it is needed and is separate from your
custom content. You can get a reference to it though by accessing the
moreNavigationController property of UITabBarController.
It just not recommended , but if you insist you will need to use a library or perhaps use a tool bar and add many UIBarButtonItems to it.
When I click the tab bar that holds my kal calendar the calendar disappears:
This is my code in viewWillAppear:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
KalViewController *calendar = [[KalViewController alloc] init];
[self.navigationController pushViewController:calendar animated:YES];
calendar.dataSource = self;
calendar.delegate = self;
[calendar reloadData];
self.tabBarController.delegate = self;
}
I also have this method:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self viewWillAppear:YES];
//Even when I comment out this line the problem stays!
}
If you need more infomation just ask.
EDIT:
You are using your tab bar controller improperly. Indeed, you should populate it with your controllers (either in Interface Builder or by setting the tab bar controller's controllers property) and let it do its job.
On another coin, note that viewWillAppear can be called multiple times, so this is the last place where you want to allocate a new controller.
If you have overridden viewWillAppear in order for your controller to show something when it s view is displayed that is not the correct approach.
EDIT:
If you create your UI programmatically, these are the steps you should follow in you application:didFinishLaunching: to setup the rootViewController:
FirstViewController *fistVC = ...
SecondViewController *secondVC = ...
ThirdViewController *thirdVC = ...
NSArray *viewControllers = [[NSArray alloc] initWithObjects:fistVC, secondVC, thirdVC, nil];
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewControllers animated:YES];
self.window.rootViewController = self.tabController;
If you build your UI in Interface Builder, you will do the same graphically, but I cannot reproduce it here.
In any case, I hope this snippet clarifies the way the tab bar controller works: you instantiate all of its controllers, put them in an array, pass the array to the tab bar controller. No need to instantiate anymore the tabbed controllers (neither in viewWillAppear nor in viewDidLoad)...
I need to have a tabbar kind of view in a view controller(say view controller 3) which is in navigation stack and will be pushed by another view controller (say view controller 2).
I added tool bar to view controller 3 with many buttons. But managing and switching between views seems difficult.
How can i add a tabbar controller in the middle of navigation stack.
please help me out.
You cnt create tabbar in the middle of view controllers, you can create tab bar structure using custom segment controller. check the link below for creating custom segment controller.
https://www.cocoacontrols.com/search?q=segment
You cannot add TabBar there - tabBar is only designed to be at the bottom.
You have to create your own ViewController and add it as subView for navigationViewController and implement whole switching methods.
There has been some discussion about this design in related questions.
The way I understand your question it's definitely possible though. I am pushing a UITabBarController onto a UIViewController that is embedded in a UINavigationController. I did everything in the interfacebuilder except for the push which I implemented programmatically.
So in ViewController.m I added an action to a button:
- (IBAction)searchForPlacesButtonPressed:(id)sender {
TabBarController *tc = [[self storyboard] instantiateViewControllerWithIdentifier:#"TabBarController"];
tc.title = #"Orte";
tc.tabBar.selectedImageTintColor = [UIColor colorWithRed:(28.0f/255.0f) green:(100.0f/255.0f) blue:(52.0f/255.0f) alpha:1.0f];
UIBarButtonItem * logo = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon_xyz"]]];
tc.navigationItem.rightBarButtonItem = logo;
Places *d = [[Places alloc] init];
for (UIViewController *vc in tc.viewControllers) {
if ([vc isKindOfClass:[MapViewController class]]) {
((MapViewController *)vc).dataHandler = d;
((MapViewController *)vc).mapViewDelegate = d;
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Karte" image:[UIImage imageNamed:#"map"] tag:0];
}
else if ([vc isKindOfClass:[TableViewController class]]) {
((TableViewController *)vc).dataHandler = d;
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Liste" image:[UIImage imageNamed:#"eye"] tag:1];
}
}
[self.navigationController pushViewController:tc animated:YES];
}
Using the tabs at the bottom you can switch between viewing the places on a map and listed in a table.
I have UIViewController with tabbar botton, and navigation bar and navigation item,
when I press on my button in navigation Item I want to load a view, I don't know how to load this view on tabbar,
would you please help me,thanks in advance!
-(IBAction) infoPage:(id)sender
{
InfoCtrol *i = [[InfoCtrol alloc] initWithNibName:#"InfoCtrol" bundle:nil];
[self.navigationController pushViewController:i animated:YES];
}
This will load the InfoCtrol controller and set the Touch UpInside Event in xib to the infoPage method
Do you mean you want to hide the tab bar when you push new controller in navigation?
If true. There is a property hidesBottomBarWhenPushed in the UIViewController class.
// Instead of adding ViewController to TabbarController, add NavigationControllers.
// Eg.
UINavigationController *NavController = [[UINavigationController alloc] initWithRootViewController:_viewCtrl1];
[watchListNavController.navigationBar setTintColor:[UIColor blackColor]];
_tabC = [[UITabBarController alloc] init];
_tabCt.viewControllers = [NSArray arrayWithObjects:NavController,_viewCtrl2,_viewCtrl3, nil];
// Now you can use Push and Pop In your _viewCtrl1.
// Do same with all the viewController
I had a navigation controller based application. And I decided to use tab bars in my application.
When the user presses at a certain tab bar item I want to display a certain view controller - and I want programmatically in my code choose which one to display.
I tried to add in the Interface Builder a navigation controller into my tab bar, but viewWillAppear of its view controller is not being called.
How can I implement this feature?
I don't know if it's the "right way", but here's how I usually do this with three tabs.
- (void)initControls {
// Create the window.
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
// Create Tab Bar.
tabCon = [[UITabBarController alloc] init];
// Local array variable that holds the viewcontrollers.
// Capacity corresponds to the number of VC's
NSMutableArray *localVCArray = [[NSMutableArray alloc] initWithCapacity:3];
MyFirstViewController *oneViewController = [[MyFirstViewController alloc] init];
UINavigationController *oneNavCon = [[UINavigationController alloc] initWithRootViewController:oneViewController];
[localVCArray addObject:oneNavCon];
[oneViewController release];
[oneNavCon release];
MySecondViewController *twoViewController = [[MySecondViewController alloc] init];
UINavigationController *twoNavCon = [[UINavigationController alloc] initWithRootViewController:twoViewController];
[localVCArray addObject:twoNavCon];
[twoViewController release];
[twoNavCon release];
MyThirdViewController *threeViewController = [[MyThirdViewController alloc] init];
UINavigationController *threeNavCon = [[UINavigationController alloc] initWithRootViewController:threeViewController];
[localVCArray addObject:threeNavCon];
[threeViewController release];
[threeNavCon release];
// Set the tab bars array of view controllers to the localVCArray
[[self tabCon] setViewControllers:localVCArray animated:YES];
// Release the localVCArray, all of its contents are now retained by tabCon.
[localVCArray release];
// Add controls to window and show.
[window addSubview:[tabCon view]];
[window makeKeyAndVisible];
}
In the init method each viewController you can do something like:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
To set the icon used in the tab bar, the title in the tab bar, and the title of you navigation item.