how to modify title and badge icon in UITabBarController - iphone

I have the following code:
tabBarViewController = [[TabBarViewController alloc] init];
mvc = [[MapViewController alloc] init];
tvc = [[TableViewController alloc] init];
tabBarViewController.viewControllers = [NSArray arrayWithObjects: tvc, mvc, nil];
However, when opening up in the simulator, I don't see any title or badge for these two UITabBar. How can I assign title and badge to these two ?

You must set each child view controller's tabBarItem property:
MapViewController* mvc = [[MapViewController alloc] init];
UIImage* mapIcon = [UIImage imageNamed:#"mapIcon.png"];
mvc.tabBarItem = [[[UITabBarItem alloc] initWithTitle:#"Map" image:mapIcon tag:0] autorelease];
mvc.tabBarItem.badgeValue = #"abc";
TableViewController* tvc = [[TableViewController alloc] init];
tvc.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0] autorelease];
mvc.tabBarItem.badgeValue = #"100";

TabBarViewController has a property to its UITabBar.
The UITabBar has the list of UITabBarItem that have the badge
[tabBarViewController.tabBar.items objectAtIndex:0].badgeValue = #"1";

why not you use this code in tvc, mvc
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.tabBarItem = [ [ UITabBarItem alloc ]
initWithTitle:#"YOUR TITLE"
image:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"IMAGE" ofType:#"png"]]
tag:1 ];
}
return self;
}

Related

Creating TabBar programmatically only for one ViewController not in AppDelegate

I want to create a tabBar in my app but only in a detailView not in the AppDelegate. I am searching how to do this in google but everything what I have figured out is in AppDelegate.m
I am trying to do something like this. This shows me a Tab bar with two buttons(without names) but I want to go from one FirstViewController with tabBar, to one of the two items SecondViewController or ThirdViewController with a navigationBar with one backItemButton for get back to the FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupTabBar];
}
- (void) setupTabBar {
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
YPProfileViewController *profile = [[YPProfileViewController alloc] init];
YPProfileViewController *profile2 = [[YPProfileViewController alloc] init];
[localViewControllersArray addObject:profile];
[localViewControllersArray addObject:profile2];
tabBars.tabBarItem = profile2;
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tabBars.view];
}
at least this works for me.
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[vc1,vc2];
tabBar.selectedIndex = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];

How to add UITabBarController programmatically (no xib file or storyboard)

I want to add a UITabBarController to my application. But I have to do it with code only. No xib files or storyboards. How to do this entirely through code?
EDIT:
_tbc = [[UITabBarController alloc] init];
aboutUsView = [[AboutUsView alloc] init];
helpView = [[HelpView alloc] init];
optionsView = [[OptionsView alloc] init];
self.navCon = [[UINavigationController alloc] initWithRootViewController:optionsView];
[self setnavigationCon:self.navCon];
[optionsView setdataLayer:self];
if ([navCon.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"Navigation Bar_reduced.png"];
[self.navCon.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
[optionsView addSelfView:window];
}
_tbc.viewControllers = [NSArray arrayWithObjects:navCon, aboutUsView, helpView, nil];
[window addSubview:_tbc.view];
Try this
AppDelegate.h
#interface AppDelegate : UIResponder <UITabBarControllerDelegate>
#property (strong, nonatomic) UITabBarController *tabBarController;
AppDeleGate.m
UINavigationController *nc1;
nc1 = [[UINavigationController alloc] init];
[nc1.navigationBar setTintColor:[UIColor blackColor]];
UIViewController *viewController1 = [[[FirstScreen alloc] initWithNibName:#"FirstScreen_ipad" bundle:nil] autorelease];
nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nc2;
nc2 = [[UINavigationController alloc] init];
[nc2.navigationBar setTintColor:[UIColor blackColor]];
UIViewController *viewController2 = [[[FullList alloc] initWithNibName:#"FullList_ipad" bundle:nil] autorelease];;
nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
UIViewController *viewController3 = [[[FavouriteView alloc] initWithNibName:#"FavouriteView_ipad" bundle:nil] autorelease];
UINavigationController *nc3;
nc3 = [[UINavigationController alloc] init];
[nc3.navigationBar setTintColor:[UIColor blackColor]];
nc3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];
UIViewController *viewController4 = [[[UpcomingFights alloc] initWithNibName:#"UpcomingFights_ipad" bundle:nil] autorelease];
UINavigationController *nc4;
nc4 = [[UINavigationController alloc] init];
[nc4.navigationBar setTintColor:[UIColor blackColor]];
nc4.viewControllers = [NSArray arrayWithObjects:viewController4, nil];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1, nc2,nc3,nc4 ,nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
ICON FOR TABBAR
In your ViewController.m file do as follow:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self.title = NSLocalizedString(#"YOUR View NAME", #"YOUR VIEW NAME");
self.tabBarItem.image = [UIImage imageNamed:#"YOUR IMAGE NAME"];
return self;
}
Add this code in your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController * fvc=[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController * svc=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController * tvc=[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
FourthViewController * fvc2=[[FourthViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
tabbar=[[UITabBarController alloc]init];
tabbar.viewControllers=[NSArray arrayWithObjects:fvc,svc,tvc,fvc2,nil];
[self.window addSubview:tabbar.view];
}
Here is the code from user1673099 in Swift (nice username btw... ;0) -
func initialize_tabs() {
var ncArr : [UINavigationController] = [UINavigationController]();
for _ in 0...3 {
let nc : UINavigationController = UINavigationController();
let vc = UIViewController();
nc.viewControllers = [vc];
let v : UIView = UIView(frame: UIScreen.mainScreen().bounds);
let redC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let greenC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let blueC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
v.backgroundColor = UIColor(red: redC, green: greenC, blue: blueC, alpha: 1);
let l : UILabel = UILabel(frame: UIScreen.mainScreen().bounds);
l.text = "Test Label";
l.textAlignment = .Center;
v.addSubview(l);
vc.view = v;
ncArr.append(nc);
}
self.tabBarController = UITabBarController();
self.tabBarController.viewControllers = ncArr;
return;
}

Error: Creating Tab-Navigation programmatically

Excuse if this problem is posted already..
I want to create combination of TabBar and NavigationBar programmatically using XCode 4.2 & iPhone SDK 5.0
It produces visual as expected..but when a TabBarItem is pressed(taped) to change to its corresponding view, it is producing error: [__NSCFString _tabBarItemClicked:]: unrecognized selector sent to instance
Here is the implementation of the AppDelegat
#import "ApplicationDelegat.h"
#import "BrightnessController.h"
#implementation ApplicationDelegat
#synthesize window;
//#synthesize bControl;
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSMutableArray *controllers = [NSMutableArray array];
UITabBarController *tbarController = [[UITabBarController alloc] init];
for (int i = 0; i <= 3; i++)
{
//self.bControl = [[BrightnessController alloc] initWithBrightness:i];
BrightnessController *bControl = [[BrightnessController alloc] initWithBrightness:i];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: /*self.bControl*/bControl];
nav.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[controllers addObject: nav];
//bControl.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"test" image:nil tag:i];
//tbarController.navigationController.delegate = self;
}
tbarController.viewControllers = controllers;
tbarController.customizableViewControllers = controllers;
tbarController.selectedIndex = 0;
tbarController.delegate = self;
// NSCFString
//tabBarItem
// Set up the window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:tbarController.view];
[self.window makeKeyAndVisible];
}
#end
I don't know why it happens and how to recover it..
Somebody help me.
If more detail is required, I can provide the source code...
Thanks in advance.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; 7:20
switch(mytabbar.selectedIndex)
{
case 0:
[imageView1 setImage:[UIImage imageNamed:#"Tab1_sel.png"]];
[imageView2 setImage:[UIImage imageNamed:#"Tab2.png"]];
[imageView3 setImage:[UIImage imageNamed:#"Tab3.png"]];
[imageView4 setImage:[UIImage imageNamed:#"Tab4.png"]];
[imageView5 setImage:[UIImage imageNamed:#"Tab5.png"]];
break;
case 1:
you can use this method and change on each tabbar click index
Two issues I see...
You assign the view of the tabBarController as a subView of the window. This is incorrect. You need to set the rootViewController of the window to be the tBarController.
Are you implementing the tab bar's delegate method tabBar:didSelectViewController:? Your error message says you're trying to send a tabBar-related method to an NSString. I suspect it's due in part to point (1). Try:
self.window.rootViewController = self.tBarController;
I typically prefer creating another tabbarview class that handles all the different view controllers, in which case, the app delegate will look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController.myTabBarController;
[self.window makeKeyAndVisible];
return;
}
And in ViewController I do this:
Header file:
#interface ViewController : UIViewController {
IBOutlet UITabBarController *myTabBarController;
}
#property (nonatomic, retain) IBOutlet UITabBarController *myTabBarController;
And in the implementation file:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
exerciseViewController *viewController1 = [[exerciseViewController alloc] init];
viewController1.title = #"Exercise";
viewController1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Exercise" image:[UIImage imageNamed:#"inbox.png"] tag:0];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];
bookViewController *viewController2 = [[bookViewController alloc] init];
viewController2.title = #"Book";
viewController2.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Book" image:[UIImage imageNamed:#"inbox.png"] tag:1];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
askViewController *viewController3 = [[askViewController alloc] init];
viewController3.title = #"Ask";
viewController3.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Ask" image:[UIImage imageNamed:#"inbox.png"] tag:2];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:viewController3];
workshopViewController *viewController4 = [[workshopViewController alloc] init];
viewController4.title = #"Workshop";
viewController4.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Workshop" image:[UIImage imageNamed:#"inbox.png"] tag:3];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:viewController4];
myTabBarController = [[UITabBarController alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, thirdNavController, fourthNavController, nil];
}
return self;
}
This is just an example...and I think it's the right way to do that.

ViewDid appear not working xcode

Hai,
I have created a tab based iRestaura project. I use 5 tab bar items. One of the tabbar name is Customer. When I tap on customer another tabbar is created with 3 viewcontroller programmatically. When I use view didload method tabbarcontroller is created successfully. But when i use view didAppear then tabbar controller is not created tabbar. In both of cases the other three viewcontroller which is created programmatically , there is not view did appear is not working. But all of case I need to use viewDidAppear method .
Plz anyone can help me...
The viewDidAppear method which not working is given bellow .....
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UITabBarController *tabBarController1=[[UITabBarController alloc] init];
CustomerListViewController *customerListViewController=[[CustomerListViewController alloc] init];
customerListViewController.title=#"Customer List";
UIImage *anImage1 = [UIImage imageNamed:#"customer.png"];
UITabBarItem *theItem1 = [[UITabBarItem alloc] initWithTitle:#"CustomerList" image:anImage1 tag:0];
customerListViewController.tabBarItem = theItem1;
SelectedCustomerViewController *selectedCustomerViewController= [[SelectedCustomerViewController alloc] init];
selectedCustomerViewController.title=#"Selected Customer";
UIImage *anImage3 = [UIImage imageNamed:#"selectedCustomer.png"];
UITabBarItem *theItem = [[UITabBarItem alloc] initWithTitle:#"Selected Customer" image:anImage3 tag:1];
selectedCustomerViewController.tabBarItem = theItem;
InvoiceListViewController *invoiceListViewController=[[InvoiceListViewController alloc] init];
invoiceListViewController.title=#"Invoice List";
UIImage *anImage2 = [UIImage imageNamed:#"invoiceNo.png"];
UITabBarItem *theItem2 = [[UITabBarItem alloc] initWithTitle:#"Invoice List" image:anImage2 tag:2];
invoiceListViewController.tabBarItem = theItem2;
NSMutableArray *controllers=[[NSMutableArray alloc] init];
[controllers addObject:customerListViewController];
// [controllers1 addObject:vc1];
[controllers addObject:selectedCustomerViewController];
[controllers addObject:invoiceListViewController];
///[controllers1 addObject:vc4];
tabBarController1.viewControllers=controllers;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[[self view] addSubview:tabBarController1.view];
for (int t=0; t<[controllers count]; t++)
{
NSLog(#"controller%#",[controllers objectAtIndex:t]);
}
}
Try this:
Firstly use the below function on your Tabbar controller .m file:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:YES];
}
And in your "viewDidLoad" function of Tabbar controller .m file, use "localNavigationController.delegate = self;" as used in given example below.
UserListing *nearBy = [[UserListing alloc] init];
nearBy.tabBarItem.image = [UIImage imageNamed:#"icoTabNearby.png"];
nearBy.tabBarItem.title = #"Nearby";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:nearBy];
localNavigationController.delegate = self;
Hope it works for you.

How to resize UITableView within UINavigationController within UITabBarViewController?

So my app has a UITabBarController. Within that, one of the tabs is a UIViewController containing a UINavigationController. Within that is a UITableViewController.
The problem is that for some reason the size of the TableView is off, and part of the bottom entry of the table is obscured by the TabBar. I've tried everything I can think of to resize the tableview but to no avail. Here are the relevant parts of the code:
AppDelegate
tabBarController = [[UITabBarController alloc] init];
LogbookViewController *firstVC = [[LogbookViewController alloc] init];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: firstVC, secondVC, thirdVC, nil];
LogbookViewController (subclass of UIViewController)
- (void)viewDidLoad {
[super viewDidLoad];
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
listController.managedObjectContext = self.managedObjectContext;
[navigationController pushViewController:listController animated:NO];
[listController release];
}
The WorkoutListTableViewController (subclass of UITableViewcontroller) currently doesn't have anything in particular in it that I would think would affect it, but here's the ViewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Workouts";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *fileName = #"short_bg.png";
NSString *filePath = [paths stringByAppendingPathComponent:fileName];
UIImage *paper = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *paper_bg = [[UIImageView alloc] initWithImage:paper];
self.tableView.backgroundView = paper_bg;
[fileName release];
[paper release];
//self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
/*UIBarButtonItem *importButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Import" style:UIBarButtonItemStylePlain target:self action:#selector(importDialogAction:)];
self.navigationItem.leftBarButtonItem = importButtonItem;
[importButtonItem release];*/
}
}
I've tried things like listController.view.frame = CGRectMake(whatever) in the LogbookViewcontroller, or self.tableView.frame - CGRectMake(whatever) in the WorkoutListTableViewController, but nothing has worked. Any ideas? I can post more of the code if that helps.
You should be added the Navigation Controller directly to the UITabBarController, then it should size appropriately:
tabBarController = [[UITabBarController alloc] init];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listController];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: navController, secondVC, thirdVC, nil];
You don't need the View Controller in the 1st tab to add a UINavigationController to the view, just go ahead and add the TableViewController as the root of the nav controller and add the nav controller into the 1st tab.