How to Display title for uitabbarcontroller - iphone

I have created uitabbarview controller using the below code
In the uitabbar controller i want to display tile for every view
Please let me know how to add title for each view ( tab bar item )
myTabBarController = [[UITabBarController alloc] init];
MyDialerViewController *aDialerViewController = [[MyDialerViewController alloc]init];
MyCallLogViewController *aCallLogViewController = [[MyCallLogViewController alloc] init];
TemplateViewController *aTemplateViewController = [[TemplateViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:aDialerViewController, aCallLogViewController, aTemplateViewController, nil];
myTabBarController.viewControllers = controllers;
myTabBarController.delegate = self;
myTabBarController.selectedIndex = 0;
[controllers release];
[aDialerViewController release];
[aCallLogViewController release];
[aTemplateViewController release];
[self.window addSubview:myTabBarController.view];
[self.window makeKeyAndVisible];

I set the title of the navcontroller directly in the appDelegate.
aDialerViewController.title = NSLocalizedString(#"Dialer Title", #"Dialer Title");
aCallLogViewController.title = #"Title";
aTemplateViewController.title = #"Title";
I'd also set it in the viewDidLoad method of those viewControllers.
Works fine for me. Remember to localize, just incase you need it in the future. (if not intended to, you should look into it)

self.viewcontroller.tabBarItem = // a UITabBarItem instance
Make an instance of UITabBarItem
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITabBarItem_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UITabBarItem
(id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
You could write that in the initializer function of each viewcontroller.

Related

Preservation and Restoration for Tabbar without Storyboard

I am using iOS 6 Preservation and Restoration (without Storyboard), it is working fine with navigation controller , but if i manually add Tabbar controller on main window , i am not getting selected tab.
eg.
ListViewController *list = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
SettingViewController *setting = [[SettingViewController alloc] initWithNibName:#"SettingViewController" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:list];
navigation.restorationIdentifier = #"NavigationControllerID";
self.tabbar = [[UITabBarController alloc] init];
self.tabbar.restorationIdentifier = #"TabbarControllerID";
self.tabbar.viewControllers = #[navigation,setting];
[[_tabbar.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(#"List", #"comment")];
[[_tabbar.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(#"Setting", #"comment")];
self.window.rootViewController = self.tabbar;
[self.window makeKeyAndVisible];
in t his case i am getting first tab selected every time.i have implatementd
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
for setting view controller.
By Adding this methods in Appdelegate
NSString * const AppDelegateRootVCKey = #"AppDelegateRootVCKey";
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
//Adding last tabbar selected index
[coder encodeObject:[NSString stringWithFormat:#"%d",self.tabbar.selectedIndex]forKey:AppDelegateRootVCKey];
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
//Setting Last tabbar selected index
NSString *selectedStr = [coder decodeObjectForKey:AppDelegateRootVCKey];
self.tabbar.selectedIndex = [selectedStr intValue];
return YES;
}

Populate Table With Help of PopOverController - Objective C

I am using a UIButton, on clicking it I want to display the contents that are present in my NSMutableArray in UITableView with the help of UIPopOverController i.e. I want a UITableView to pop up whose cells show the contents of my NSMutableArray.
I am using the following lines of code:
UITableViewController *table= [[UITableViewController alloc]init];
table.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:table];
self.popoverController = popover;
popoverController.delegate = self;
NSString *hDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *hFilePath = [hisDir stringByAppendingPathComponent:#"hhhh.txt"];
NSArray *array = [NSArray arrayWithContentsOfFile:hFilePath ];
NSMutableArray *kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[kkkk addObjectsFromArray:[dict allKeys]];
}
table = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:table];
table.hidden = YES;
[window makeKeyAndVisible];
I am unable to get the UITableView to pop up on the press of my UIButton. Can anyone help me to sort it out?
One way to show the UITableView in the UIPopOverController is by creating a new UIViewController class. And invoking it in initWithContentViewController method of UIPopOverController.
1. Create a new UIViewController or UITableViewController class. Create an instance of it.
2. Use the instance in the initWithContentViewController method of UIPopOverController.
3. Mention from where it should "pop"
For Example in your Button action :
-(IBAction)yourButtonAction:(id)sender
{
YourNewViewController*newVC=[[YourNewViewController alloc]init];
UIPopoverController*somePopOver=[[UIPopoverController alloc]initWithContentViewController:catergoryVC]; //Tell which view controller should be shown
[somePopOver setPopoverContentSize:CGSizeMake(200, 200)]; // set content size of popover
[somePopOver presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //From where it should "pop"
}
It seems you want to present it from a UIBarButtonItem so instead of presentPopoverFromRect use presentPopoverFromBarButtonItem
[somePopOver presentPopoverFromBarButtonItem:yourBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to display popover on press of button click, then first add your button in viewcontroller, display that view controller as follows:
In app delegate write code:
MyViewController *viewController = [[MyViewController alloc] init];
[self.window addSubView:viewController.view];
In MyViewController add button and provide target to that button displayPopup as follows:
-(void)displayPopup:(id)sender
{
UITableViewController *tblViewPopover = [[UITableViewController alloc] init];
tblViewPopover.tableView.delegate = self;
tblViewPopover.tableView.dataSource = self;
tblViewPopover.tableView.backgroundColor = [UIColor whiteColor];
tblViewPopover.tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
float theWidth = 280;
tblViewPopover.contentSizeForViewInPopover = CGSizeMake(theWidth,200);
if(m_popOvrController){
[m_popOvrController dismissPopoverAnimated:NO];
[m_popOvrController release];
m_popOvrController=nil;
}
m_popOvrController = [[UIPopoverController alloc] initWithContentViewController:tblViewPopover];
[tblViewPopover release];
[m_popOvrController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
and you can use tableview delegate methods to display data in tableview.
I think, UIPopoverViewController is initialized using UIViewController and here you are using UIView(UITableView).
Can you please try using UITableViewController instead?
Also, if things does not work according to plan when you create it using the code try using an XIB explicitely.
This should help.

issue with pushing to navigation bar

I have the following code:
- (void)setupBook
{
if (self.bookNavigationController) {
BookViewController *bookVC = [[BookViewController alloc] initWithTemplate:x];
self.myBookVC = bookVC;
[bookVC release];
myBookVC.pageTitle = #"My Book";
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:myBookVC];
self.bookNavigationController = nController;
[nController release];
}
}
and then in the other parts of the code I have:
[self.someOtherNavigationController pushViewController:self.bookVC];
however now when I try to present self.bookNavigationController as a modal view controller, it is as the myBookVC is not there. why is this? It just shows up an empty view with a navigation bar.
When to first create your view controller you are assigning it to a property, however when assign to as the nav controller's rootViewController you are referencing an ivar. Use the property instead when assigning the rootViewController.
BookViewController *bookVC = [[BookViewController alloc] initWithTemplate:x];
bookVC.pageTitle = #"My Book";
self.myBookVC = bookVC;
[bookVC release];
UINavigationController *nController = [[UINavigationController alloc] initWithRootViewController:self.myBookVC];
self.bookNavigationController = nController;
[nController release];

UITabBarController Does not shows tabbarItems name

I am creating app based on UTabbarController. I have creates that tab bar programmatically. Everything is running fine except I can not see the tabBatItem title. I have initialized everything properly, but when application launches all I can see is the first tabbar title. but if I select 2nd tabbaritem or so on I can see their names. I don't know whats going wrong here. Here is my code. Please let me know if I made any mistake.
Thanks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
HomeViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
UINavigationController*navController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navController1.title=#"Home";
[viewController1 release];
TrainerTableViewController *viewController2 = [[TrainerTableViewController alloc] initWithNibName:#"TrainerTableViewController" bundle:nil];
UINavigationController*navController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navController1.title=#"Trainer";
[viewController2 release];
SettingsTableViewController *viewController8 = [[[SettingsTableViewController alloc] initWithNibName:#"SettingsTableViewController" bundle:nil] autorelease];
UINavigationController*navController8=[[[UINavigationController alloc]initWithRootViewController:viewController8]autorelease];
navController1.title=#"Settings";
AboutUsViewController *viewController9 = [[[AboutUsViewController alloc] initWithNibName:#"AboutUsViewController" bundle:nil] autorelease];
UINavigationController*navController9=[[[UINavigationController alloc]initWithRootViewController:viewController9]autorelease];
navController1.title=#"About Us";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2,navController8, navController9, nil];
[navController1 release];
[navController2 release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You can do this by including the code below inside the .m file of the view controller for each tab bar item. The code also includes how to change the image on the tab bar.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
self.title = #"Apply Now";
self.tabBarItem.image = [UIImage imageNamed:#"tbApplyNow.png"];
}
return self;
}
The best way to solve this problem is to set title of viewController not of navigationController
viewController1.tabBarItem.title = #"CohesiveSelf";
Do this for all tabs.
You're only setting the title for navController1. For each nab controller you create you need to set the title for that one.
Try this:
CHANGE:
navController1.title=#"Trainer";
TO
navController2.title=#"Trainer";
CHANGE:
navController1.title=#"Settings";
TO
navController8.title=#"Settings";
CHANGE:
navController1.title=#"About Us";
TO
navController9.title=#"About Us";
Also not you are not releasing navController8 or 9 which will cause in a memory leak
You are actually setting the title for the NavigationControllers. Also, watch that your code sets the title of navController1 multiple times, rather than setting it for the others.
You can set the titles for your tabBar by setting up tabBarItems for each controller.
You have the option of subclassing, or just including this in the application:didFinishLaunchingWithOptions: method in your AppDelegate.
Here's an example:
UITabBarItem *tbi1 = [navController1 tabBarItem];
[tbi1 setTitle:#"Home"];
UIImage *i1 = [UIImage imageNamed:#"hometabicon.png"];
[tbi1 setImage:i1];
This will set the title of tab1 to 'Home' and will set the tabBar icon to a file named 'hometabicon.png'.
You can repeat the same pattern for each of the other tabs.

Can't get Secondary UITableViewController to display inside a UITabBarController

I've programmatically created a UITabBarController that is loaded in my App Delegate like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
myTableViewController = [[MyTableViewController alloc] init];
UINavigationController *tableNavController = [[[UINavigationController alloc] initWithRootViewController:myTableViewController] autorelease];
myTableViewController.title = #"Tab 1";
[myTableViewController release];
mySecondTableViewController = [[MySecondTableViewController alloc] init];
UINavigationController *table2NavController = [[[UINavigationController alloc] initWithRootViewController:mySecondTableViewController] autorelease];
mySecondTableViewController.title = #"Tab 2";
[mySecondTableViewController release];
tabBarController.viewControllers = [NSArray arrayWithObjects:tableNavController, table2NavController, nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
Now the issue I have is that I can get into the views no problem, but when I try and click onto any item in the Table View, I can't get a secondary table view to appear in any tab. The tabs work absolutely fine, just the secondary views. I'm using the code below in my myTableViewController to run when selecting a specific row (the code reaches the HELP line, and crashes)...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
// this gets the correct view controller from a list of controllers
SecondaryViewController *svc = [self.controllers objectAtIndex:row];
/*** HELP NEEDED WITH THIS LINE ***/
[self.navigationController pushViewController:svc animated:YES];
}
Simply put, I'm trying to switch views to the new view controller whilst keeping the tabs available and using the navigation to go back and forth (like in the iTunes App).
Simply put, I was creating the controller array, self.controllers, and adding objects, and then releasing the objects.
If you do not release the object until the array is released, it appears to work no problem.
You've not included in your question how you initialize the self.controllers array.
I suspect this array is not filled with initialized SecondaryViewController objects.
EDIT (Added Code example that works for me):
the .h file:
#interface FirstLevelViewController : UITableViewController {
NSArray *controllers;
}
#property (nonatomic, retain) NSArray *controllers;
#end
and the .m file:
#implementation FirstLevelViewController
#synthesize controllers;
- (void)viewDidLoad {
self.title = #"First Level";
NSMutableArray *array = [[NSMutableArray alloc] init];
// Disclosure Button
DisclosureButtonController *disclosureButtonController =
[[DisclosureButtonController alloc]
initWithStyle:UITableViewStylePlain];
disclosureButtonController.title = #"Disclosure Buttons";
disclosureButtonController.rowImage = [UIImage
imageNamed:#"disclosureButtonControllerIcon.png"];
[array addObject:disclosureButtonController];
[disclosureButtonController release];
// deleted further adds to array ...
self.controllers = array;
[array release];
[super viewDidLoad];
}