How to load a tabbar controller xib using navigation controller - iphone

I am new to ios, I had a problem with tabbar controller. I am using two tab bar controller in my project.One is loaded at app lunch and it is working good.I want to load another at didselect row.How to do this. I have done many experiments but nothing works.

-(void) hidetabbar {
[UIView animateWithDuration:0.5
animations:^{
for(UIView *view in tabBarController.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
if (hiddenTabBar) {
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
} else {
if (hiddenTabBar) {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
}
}];
hiddenTabBar = !hiddenTabBar;
}
when u r clicking on the table view did select row hide the tabbar in the viewcontroller that u r sending
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
[((AppDelegate*)[[UIApplication sharedApplication]delegate]) hidetabbar];
}
-(void)tabBarControllerView
{
tabBarController = [[UITabBarController alloc] init];
tabBarController.view.backgroundColor = [UIColor blackColor];
tabBarController.delegate = self;
//Add some tabs to the controller...
//----First tab----//
//-----second Tab -----//
//------3rd tab--//
//-----4th tab bar--------//
//-----5th tab bar--------//
[self.view addSubview:tabBarController.view];
[navigationController pushViewController:tabBarController animated:YES];
tabBarController.tabBar.tag=100;
tabBarController.view.hidden = NO;
}
- (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController
{
if (tabBarControllers.selectedIndex == 0)
{
}
else if (tabBarControllers.selectedIndex == 1)
{
}
else if (tabBarControllers.selectedIndex == 2)
{
}
else if (tabBarControllers.selectedIndex == 3)
{
}
else if (tabBarControllers.selectedIndex == 4)
{
}
}
hide main tab bar in that view controller where u r doing the did select table and add another tab bar..
try some thing like this it may help u

Add the following Code in didSelect event
UITabBarController *tabBarController = [[UITabBarController alloc]init];
NSArray*tabBarimageArray=[NSArray arrayWithObjects:#"firstTabImage.png",#"secondTabImage.png", nil];
YourFirstTabRootViewController *firstVc = [[YourFirstTabRootViewController alloc]initWithNibName:#"YourFirstTabRootViewController" bundle:nil];
UINavigationController *firstNavigationController=[[UINavigationController alloc]initWithRootViewController:firstVc];
YourSecondTabRootViewController *secondVc = [[YourSecondTabRootViewController alloc]initWithNibName:#"YourFirstTabRootViewController" bundle:nil];
UINavigationController *secondNavigationController=[[UINavigationController alloc]initWithRootViewController:secondVc];
NSArray *VCs = [[NSArray alloc] initWithObjects:firstNavigationController,secondNavigationController nil];
NSArray *names = [NSArray arrayWithObjects:
NSLocalizedString(#"Tab1", #""),
NSLocalizedString(#"Tab2", #""),
nil];
NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:[VCs count]];
NSInteger index = 0;
for (id controller in VCs) {
UINavigationController * navController = controller ;
// THIS SETS UP THE TAB BAR ITEMS/IMAGES AND SET THE TAG FOR TABBAR_ITEM_TAGS
NSString *tabName = [names objectAtIndex:index];
UIImage *tabImage = [UIImage imageNamed:[NSString stringWithFormat:[tabBarimageArray objectAtIndex:index]]];
navController.title = tabName;
UITabBarItem *tempTab = [[UITabBarItem alloc] initWithTitle:tabName
image:tabImage
tag:index];
navController.tabBarItem = tempTab;
[tabBarViewControllers addObject:navController];
index ++;
}
[ tabBarController setViewControllers:tabBarViewControllers];
[self presentModalViewController:tabBarController animated:YES];

Related

How to hide/Remove the search bar on Contact Picker

I am adding a contact picker in my app, however, I do not want the search functionality.
How to hide/Remove the search bar on Contact Picker (ABPeoplePickerNavigationController)?
static BOOL foundSearchBar = NO;
- (void)findSearchBar:(UIView*)parent mark:(NSString*)mark {
for( UIView* v in [parent subviews] ) {
//if( foundSearchBar ) return;
NSLog(#"%#%#",mark,NSStringFromClass([v class]));
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
v.hidden=YES;
// foundSearchBar = YES;
break;
}
if( [v isKindOfClass:[UITableView class]] ) {
CGRect temp =v.frame;
temp.origin.y=temp.origin.y-44;
temp.size.height=temp.size.height+44;
v.frame=temp;
//foundSearchBar = YES;
break;
}
[self findSearchBar:v mark:[mark stringByAppendingString:#"> "]];
}
}
call above method after picker is presented as below:
-(void)showPeoplePickerController
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty],[NSNumber numberWithInt:kABPersonAddressProperty],nil];
picker.displayedProperties = displayedItems;
// Show the picker
[self presentViewController:picker animated:YES completion:nil];
[self findSearchBar:[picker view] mark:#"> "];
[picker release];
}
-(void)showAddressBook {
ABPeoplePickerNavigationController *addressBook = [[ABPeoplePickerNavigationController alloc] init];
[addressBook setPeoplePickerDelegate:self];
addressBook.delegate = self;
addressBook.navigationBar.topItem.title = #"iPhone Contacts";
UIView *view = addressBook.topViewController.view;
for (UIView *v in view.subviews) {
if ( [v isKindOfClass:[UITableView class]] ) {
CGRect temp = v.frame;
temp.origin.y = temp.origin.y - 44;
temp.size.height = temp.size.height + 44;
v.frame = temp;
}
}
[addressBook release];
}
- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([navigationController isKindOfClass:[ABPeoplePickerNavigationController class]]) {
UISearchDisplayController *searchDisplayController = navigationController.topViewController.searchDisplayController;
[searchDisplayController.searchBar setHidden:YES];
}
}

The tabBarController is not displayed

i want to make tabbarcontroller programatically. The tabBarController is not displayed in the page. can anyone tell that whts going wrong.and can we make more than one tabbarcontroller in an application
ViewController.m
- (void)viewDidLoad
{
report=[[UIViewController alloc]initWithNibName:#"ViewController" bundle:nil];
View1 *template=[[View1 alloc]initWithNibName:#"View1" bundle:nil];
View2 *acc=[[View2 alloc]initWithNibName:#"View2" bundle:nil];
View3 *four=[[View3 alloc]initWithNibName:#"View3" bundle:nil];
View4 *five=[[View4 alloc]initWithNibName:#"View4" bundle:nil];
nav1=[[UINavigationController alloc]initWithRootViewController:report];
nav2=[[UINavigationController alloc]initWithRootViewController:template];
nav3=[[UINavigationController alloc]initWithRootViewController: acc];
nav4=[[UINavigationController alloc]initWithRootViewController:four];
nav5=[[UINavigationController alloc]initWithRootViewController:five];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:#"Title" image:[UIImage imageNamed:#"singleicon.png"] tag:0];
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:#"Reports" image:[UIImage imageNamed:#"doubleicon.png"] tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:#" New " image:[UIImage imageNamed:#"clockicon.png"] tag:2];
UITabBarItem *item3=[[UITabBarItem alloc]initWithTitle:#"four" image:[UIImage imageNamed:#"dependenticon.png"] tag:3];
UITabBarItem *item4=[[UITabBarItem alloc]initWithTitle:#"five" image:[UIImage imageNamed:#"toolicon.png"] tag:4];
nav1.tabBarItem = item;
nav2.tabBarItem = item1;
nav3.tabBarItem = item2;
nav4.tabBarItem=item3;
nav5.tabBarItem=item4;
//[item1 setBadge:#"25"];
self.tabBarController=[[UITabBarController alloc]init];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];
self.report = self.tabBarController;
// [self.report makeKeyAndVisible];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Use below code:
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];
self.window.rootViewController = self.tabBarController;
UPDATE:
also for Hide and Show the UITabBar then use bellow code ..
just put this methods in AppDelegate.m file and when you want to hide tabbar at that time just create AppDelegate object and call bellow hideTabBar method
- (void) hideTabBar:(UITabBarController *) tabbarcontroller {
int height = 480;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
for(UIView *view in tabbarcontroller.view.subviews) {
if([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
}
else {
[view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y, 320, 436)];
}
}
[UIView commitAnimations];
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller {
int height = 436;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
for(UIView *view in tabbarcontroller.view.subviews) {
if([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
}
else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, height)];
}
}
[UIView commitAnimations];
}
put all the coding in the action of the button and push the tabBarController like that:-
[self.navigationController pushViewController:tabBarController animated:YES];
If you want to add UITabBarController programmatically, then you need to add your tabbarcontroller to your ViewController. you need to use this line,
[self.view addSubview:self.tabBarController.view];
You need to add tabBarController in view you have missed the one line
i just did this
[self.navigationController pushViewController:tabBarController animated:YES];

custom tab bar controller not working?

I am trying to build a custom tab bar controller but for some reason the views will not switch... The initial view is loaded properly. Here is my init method:
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
initWithNibName:#"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
initWithNibName:#"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
initWithNibName:#"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
initWithNibName:#"LocationsViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;
return self;
}
Like I said, this will display the selected controller properly, however when the app launches and I try to switch views with the tab bar, the subview just become grey... I have been looking through several tutorials to try to figure out this issue, but it seems that I am doing it exactly the same. I have also checked the IB file to make sure my tabs are connected properly, they are. The following is the code to switch items:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == locationsTabBarItem) {
UIViewController *locationsController = [viewControllers objectAtIndex:0];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:locationsController.view];
self.selectedController = locationsController;
}
else if (item == accountsTabBarItem) {
UIViewController *accountsController = [viewControllers objectAtIndex:1];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:accountsController.view];
self.selectedController = accountsController;
}
else if (item == barTabTabBarItem) {
UIViewController *barTabController = [viewControllers objectAtIndex:2];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:barTabController.view];
self.selectedController = barTabController;
}
else {
UIViewController *moreController = [viewControllers objectAtIndex:3];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:moreController.view];
self.selectedController = moreController;
}
}
try With this one
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.selectedIndex = 0;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];

when UISegmentcontrol item changing, why it will hide customBadge in IOS 5?

I have added custom Badge on UISegmentControl. In Xcode 4.0.2 SDK 4.3 its working fine. But in Xcode 4.2 SDK 5.0 custom badge is not shown when another item is selected. I dont know why this happens? I'm using following code for IOS 4.3 Xcode 4.0.2:
- (void)viewDidLoad {
[super viewDidLoad];
super.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"wallpaper.png"]];
NSLog(#"Root View Loaded");
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Home",#"Surveys",#"Results",#"Create",#"Settings",
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 310, 40);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor darkGrayColor];
segmentedControl.momentary = NO;
segmentedControl.highlighted = YES;
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
NSArray *segmentBarArray = [NSArray arrayWithObjects:
segmentBarItem,nil];
[[self appDelegate] setSegmentedControl:segmentedControl];
[[self appDelegate] setSegmentBarArray:segmentBarArray];
[self setToolbarItems:[[self appDelegate] segmentBarArray] animated:NO];
[[self navigationController] setToolbarHidden:NO animated:YES];
[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
int surveycount = [[self appDelegate] getUnreadSurveyCount];
surveyCountBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:#"%d",surveycount]];
[surveyCountBadge setFrame:CGRectMake(105, -10, surveyCountBadge.frame.size.width, surveyCountBadge.frame.size.height)];
[segmentedControl addSubview:surveyCountBadge];
if (surveycount == 0) {
[surveyCountBadge setHidden:YES];
}else{
[surveyCountBadge setHidden:NO];
}
}
-(void)segmentAction:(id) sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"selected index is %d",segmentedControl.selectedSegmentIndex);
if (segmentedControl.selectedSegmentIndex == 0) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else if(segmentedControl.selectedSegmentIndex ==1){
surveyForMeViewController = [[SurveyForMeView alloc] initWithNibName:#"SurveyForMeView" bundle:nil];
[self.navigationController pushViewController:surveyForMeViewController animated:YES];
} else if(segmentedControl.selectedSegmentIndex ==2){
resultViewController = [[ResultView alloc] initWithNibName:#"ResultView" bundle:nil];
[self.navigationController pushViewController:resultViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==3) {
newSurveyViewController = [[NewSurveyView alloc] initWithNibName:#"NewSurveyView" bundle:nil];
[self.navigationController pushViewController:newSurveyViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==4) {
settingsViewControlle = [[SettingsView alloc] initWithNibName:#"SettingsView" bundle:nil];
[self.navigationController pushViewController:settingsViewControlle animated:YES];
}
}
Any thing else should I mention in this?
You are adding a subview to a segmentedControl. As you don't know how the system redraws the control when another item is selected, you cannot say if your view will be removed or not. Let the control, button or segmentedControl do its work and if you want to add a badge, add it as a subview of its parent.
[surveyCountBadge setFrame:CGRectMake(105.0f+segmentedControl.frame.origin.x,
-10.0f+sementedControl.frame.origin.y, surveyCountBadge.frame.size.width,
surveyCountBadge.frame.size.height)];
[[segmentedControl superView] addSubview: surveyCountBadge];

How to hide the tab bar on click of a tab in an iPhone app

I am doing a multi view app, in that I have 4 tabs, and I have view controllers in each tab. In one tab I have grouped table view controller, on click of that tab it will go to that grouped table view. Every thing is going fine.
But last row of the table is hidden under tab bar, so I need to hide the tab bar when I enter into that screen. How can I do this?
I am using this in Appdelegate to create tabs programmatically.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *localNavigationController;
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:5];
//add first tab View Controller
RootViewController *ViewController;
ViewController = [[RootViewController alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:ViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[ViewController release];
//add second tab View Controller
StudentDataEntry *GroupViewController;
GroupViewController = [[StudentDataEntry alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc]
initWithRootViewController:GroupViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[GroupViewController release];
}
if your last row is not visible in your view then there is no need to hide tab bar for that you have to make your table view height according to that, a tab bar is 48 px so minus this 48 px height from your table view height and also if there is a navigation bar at the top then also minus 44 more px from height then it will be visible. And also you can set content inset for table view to make it visible.
Try this
yourviewcontroller.hidesBottomBarWhenPushed=YES;
Try this:
BOOL hiddenTabBar = NO;
- (void) hidetabbar {
NSArray *array = self.tabBarController.view.subviews;
NSLog(#"array SubView %#",array);
[UIView animateWithDuration:1.0 delay:0.0f options:UIViewAnimationCurveLinear animations:^(){
for(UIView *view in self.tabBarController.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
if (hiddenTabBar) {
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
} else {
if (hiddenTabBar) {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
}
} completion:^(BOOL isfinsihed){
hiddenTabBar = !hiddenTabBar;
}];
}