I can not set title to my programatically tab bar controller - objective-c

I try to create tab bar controller as programatically. It is ok but I can not set title to tab bar items.. How can I do this?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
tab1 = [[ZiyaretFormTab1 alloc] initWithNibName:#"ZiyaretFormTab1" bundle:nil];
tab2 = [[ZiyaretFormTab2 alloc] initWithNibName:#"ZiyaretFormTab2" bundle:nil];
tab3 = [[ZiyaretFormTab3 alloc] initWithNibName:#"ZiyaretFormTab3" bundle:nil];
tab4 = [[ZiyaretFormTab4 alloc] initWithNibName:#"ZiyaretFormTab4" bundle:nil];
tab5 = [[ZiyaretFormTab5 alloc] initWithNibName:#"ZiyaretFormTab5" bundle:nil];
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:#"title" image:nil tag:0];
tab1.tabBarItem = item;
myTabBarController.viewControllers = [NSArray arrayWithObjects: tab1, tab2,tab3,tab4,tab5,nil];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}

UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
1 in this line means, that you try to get second object from array.
Also UITabBarItem has nice method initWithTitle:image:tag:. And this is link to documenttion its very helpfull.
My solve:
1) Create some items with:
initWithTitle:image:tag:
2) Add them to your tabbar with tab bar method:
- (void)setItems:(NSArray *)items animated:(BOOL)animated

Related

Trying to add BarButtonItem to programmatically created NavigationController

I have pushed a view controller embedded in a navigation controller after tapping a button in my app. I am trying to add a "SAVE" button in the right corner of the nav bar. However, with this code it is not showing up. My assumption is that it is not showing up because nc.navigationItem.rightBarButtonItem should be placed in viewWillAppear. Since nc is programmatically created and does not have a class file with viewWillAppear how can I do this?
Here is my code:
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
FXFormViewController *vc = [[FXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
// WHY IS THIS BUTTON NOT BEING ADDED?
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
[self presentViewController:nc animated:YES completion:nil];
}
The UINavigationController uses the -navigationItem of the currently displayed view controller. Try this instead:
vc.navigationItem.rightBarButtonItem = ...
Here you can take advantage of a powerful feature of OOP called Inheritance.
Create a Class say MyFXFormViewController inherited from FXFormViewController.
MyFXFormViewController.h
#import "FXFormViewController.h"
#interface MyFXFormViewController : FXFormViewController
#end
MyFXFormViewController.m
#import "MyFXFormViewController.h"
#implementation MyFXFormViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = saveBarButton;
}
And your previous code should be like
- (IBAction)editProfileButtonTapped:(UIButton *)sender {
MyFXFormViewController *vc = [[MyFXFormViewController alloc] init];
vc.formController.form = [[PersonalContactInfoForm alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
nc.navigationBar.translucent = NO;
nc.navigationBar.barTintColor = [UIColor colorWithRed:(105/255.0)
green:(210/255.0)
blue:(231/255.0)
alpha:1.0];
[self presentViewController:nc animated:YES completion:nil];
}

Set view controller as the first controller in UITabBarController

I have this code in my appDelegate.m that implements a UINavigationController with a UITabBarController:
FristViewController *primeiro = [[FristViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] init];
[nav1 pushViewController:primeiro animated:YES];
FilesViewController *segundo = [[FilesViewController alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] init];
[nav2 pushViewController:segundo animated:YES];
InfoViewController *terceiro = [[InfoViewController alloc] init];
UINavigationController *nav3 = [[UINavigationController alloc] init];
[nav3 pushViewController:terceiro animated:YES];
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.viewControllers = [NSArray arrayWithObjects:nav2, nav1, nav3, nil];
nav1.tabBarItem.image = [UIImage imageNamed:#"tab1.png"];
nav2.tabBarItem.image = [UIImage imageNamed:#"tab2.png"];
nav3.tabBarItem.image = [UIImage imageNamed:#"tab3.png"];
self.window.rootViewController = tab bar;
It's all right with this code, in my case when I beginning my app appearing on the first controller is FilesViewController because it is the first in the order of Tab Bar, but in my case I would like the first controller was the FristViewController without changing the order of items in the tab bar, how can I do this?
Go to your storyboard or xib where you created your TabBar. Drag the First controller to 1st position. Or programmatically you could set tabBar.selectedIndex = 0;

UITabBarController delegate didselect Item in UIViewContoller

I have added UITabBarController in appdelegate.m. Please check the below code
DiViewController *contact = [[DiViewController alloc] initWithNibName:#"DiViewController" bundle:nil];
UINavigationController *contactNav = [[UINavigationController alloc] initWithRootViewController:contact];
contactNav.navigationBar.barStyle = UIBarStyleBlack;
self.tabController = [[UITabBarController alloc] init];
CGRect tabframe = self.tabController.tabBar.frame;
tabframe.origin.y -= 10;
tabframe.size.height += 10;
self.tabController.tabBar.frame = tabframe;
self.tabController.delegate = self;
self.tabController.viewControllers = #[contactNav];
Then i have arun DiViewController.xib in simulator bottom tab bar opened. Then i have click tabbar item.didSelectViewController working in appdeletgate.m. But i want in DiViewController.m. Ho can i get didSelectViewController in DiViewController.m. i have used same below in DiViewController.m but not working.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
}

UINavigationController with UISegmentedControl in AppDelegate to switch rootviewcontroller of UINavigationController

I would like to create an app with a navigation controller as a window root view controller and a segmented control in a title view of the navigation controller to switch its root view controller
Problem: segmented control is not present after adding it to nag controller
Code:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
FirstViewController * fc = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:fc];
[fc release];
self.window.rootViewController = nc;
NSArray * array = [NSArray arrayWithObjects:#"GPS",#"List",#"Map", nil];
UISegmentedControl * sc = [[UISegmentedControl alloc] initWithItems:array];
sc.frame = CGRectMake(0, 0, 250, 50);
sc.segmentedControlStyle = UISegmentedControlStylePlain;
[nc.navigationItem setTitleView:sc];
[sc release];
[nc release];
[self.window makeKeyAndVisible];
return YES;
}
You are setting titleView for NavigationItem instead set Item for it.
NSArray * array = [NSArray arrayWithObjects:#"GPS",#"List",#"Map", nil];
UISegmentedControl * sc = [[UISegmentedControl alloc] initWithItems:array];
sc.frame = CGRectMake(0, 0, 250, 50);
sc.segmentedControlStyle = UISegmentedControlStylePlain;
To set in title view use this line
[ViewController.navigationItem setTitleView:sc];
To set Left or Right You can not directly add Item to Navigation bar, so create BarButtonItem with your segmentControl
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:sc];
add it to ViewController not to NavigationController
[ViewController.navigationItem setRightBarButtonItem:segmentItem];

UITabBarController Cropped

I have a button in a viewcontroller that loads a motherViewController. This seems to be working correctly but for some reason the newly introduced view is so tall that the content at the bottom of are cropped.
- (IBAction)LoginButton:(UIButton *)sender
{
FirstViewController *FirstView = [self.storyboard instantiateViewControllerWithIdentifier:#"First"];
SecondViewController *SecondView = [self.storyboard instantiateViewControllerWithIdentifier:#"Second"];
ThirdViewController *ThirdView = [self.storyboard instantiateViewControllerWithIdentifier:#"Third"];
FourthViewController *FourthView = [self.storyboard instantiateViewControllerWithIdentifier:#"Fourth"];
FifthViewController *FifthView = [self.storyboard instantiateViewControllerWithIdentifier:#"Fifth"];
FirstView.Username = self.UsernameBox.text;
UINavigationController *FirstNavController = [[UINavigationController alloc]init];
[FirstNavController pushViewController:FirstView animated:NO];
UINavigationController *SecondNavController = [[UINavigationController alloc]init];
[SecondNavController pushViewController:SecondView animated:NO];
UINavigationController *ThirdNavController = [[UINavigationController alloc]init];
[ThirdNavController pushViewController:ThirdView animated:NO];
UINavigationController *FourthNavController = [[UINavigationController alloc]init];
[FourthNavController pushViewController:FourthView animated:NO];
UINavigationController *FifthNavController = [[UINavigationController alloc]init];
[FifthNavController pushViewController:FifthView animated:NO];
tabBar = [[UITabBarController alloc]init];
tabBar.viewControllers = [NSArray arrayWithObjects:FirstView, SecondView, ThirdView, FourthView, FifthView, nil];
[self.view addSubview:tabBar.view];
}
I don't whether i'm right or not. But here's the answer, you are trying to add UIViewController (UITabbarController) to your ViewController. So you need to position it in your view, for example
tabbar.view.frame = CGRectMake(0, 0, 320, 460);
But only thing i don't know its how it's perfectly positioned, when we are adding to window. Try your code by setting frame for your tabbarcontroller.
try this -
tabBar.tabBar.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;