Set view controller as the first controller in UITabBarController - objective-c

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;

Related

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;

App crashes on start

Hi I was doing some testing earlier and my app was running just fine. I wanted to do some more testing so I decided to remove the app from my device and then reinstall it by running.
Well now for some reason I get to the stage where my splash screen shows up and then it crashes and I get the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
This obviously means theres an array out of bounds correct? But why now and how can I found out where and what view controller this is happening on? How could it run before and now all of a sudden when I try reinstalling the app through running it again I get this error?
Thanks
EDIT
The error is with the array in the following code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
exploreViewController *view1 = [[exploreViewController alloc] initWithNibName:#"exploreViewController" bundle:nil];
view1.title= #"Explore";
Upcoming *view2 = [[Upcoming alloc] initWithNibName:#"Upcoming" bundle:nil];
view2.title = #"Upcoming";
calcViewController *view3 = [[calcViewController alloc] initWithNibName:#"calcViewController" bundle:nil];
view3.title = #"Calc";
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:view2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:view3];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nil];
self.tabBarItem = [[[UITabBarItem alloc] init] autorelease];
NSArray *tabBarItems = self.tabBarController.tabBar.items;
UIImage *tab1 = [UIImage imageNamed:#"85-trophy.png"];
UIImage *tab2 = [UIImage imageNamed:#"12-eye.png"];
UIImage *tab3 = [UIImage imageNamed:#"237-key.png"];
NSArray *tabBarImages = [[[NSArray alloc] initWithObjects:tab1, tab2, tab3,nil]autorelease];
NSInteger tabBarItemCounter;
for (tabBarItemCounter = 0; tabBarItemCounter < [tabBarItems count]; tabBarItemCounter++)
{
tabBarItem = [tabBarItems objectAtIndex:tabBarItemCounter];
tabBarItem.image = [tabBarImages objectAtIndex:tabBarItemCounter];
}
Well, the reason for this crash is the following:
You are adding five items to your tabBar (nav1, nav2, nav3, nav4, nav6), but you only have three images for your tabs (tab1, tab2, tab3). So when the for loop reaches the fourth tab it crashes because tabBarImages only contains three objects.
Apart from that your code looks a bit messy - which could be the reason for not seeing the error on first sight.
// edit
You are complicating things - just try the following piece of code
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav1.title = #"Explore";
nav1.tabBarItem.image = [UIImage imageNamed:#"85-trophy.png"];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav2.title = #"Upcoming";
nav2.tabBarItem.image = [UIImage imageNamed:#"12-eye.png"];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav3.title = #"Calc";
nav3.tabBarItem.image = [UIImage imageNamed:#"237-key.png"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:nav1, nav2, nav3, nil]];
[nav1 release];
[nav2 release];
[nav3 release];

I can not set title to my programatically tab bar controller

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

How to swap MAAttachedWindow's view

How can I swap the view of my MAAttachedWindow with the window visible?
defaultViewController = [DefaultViewController alloc] initWithNibName:#"DefaultView" bundle:nil];
attachedWindow = [[MAAttachedWindow alloc] initWithView:[defaultViewController view]
attachedToPoint:point
inWindow:nil
onSide:MAPositionBottom
atDistance:25.0];
How would I swap in AnotherViewController's view?
After a good night's sleep, it took 5-minutes to figure this out!
NSViewController *vc = [[NSViewController alloc] initWithNibName:#"MainView"
bundle:nil];
view = vc.view;
[vc release];
// Setup firstViewController here, addSubview and set current = firstViewController.view
attachedWindow = [[MAAttachedWindow alloc] initWithView:view
attachedToPoint:pt
inWindow:nil
onSide:MAPositionBottom
atDistance:5.0];
Then in my swap view method, simply:
-(void)swapView
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
// Resize our host view
[view setFrameSize:secondViewController.view.frame.size];
// Replace the current view
[view replaceSubview:current with:secondViewController.view];
// Resize our attachedWindow
[attachedWindow setFrame:secondViewController.view.frame display:YES];
[secondViewController release];
[current release];
}

how to set individual tabbaritem icons in uitabbarcontroller in cocoa

I was answered how to set images in general for a uitabbarcontroller. however my uitabbarcontroller is an array of views that looks like:
tabBarController = [[UITabBarController alloc] init];
viewTab1controller = [[ViewTab1Controller alloc] initWithNibName:#"ViewTab1" bundle:nil];
viewTab1controller.title = #"Schedules";
navigationTab1Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab1controller] autorelease];
[viewTab1controller release];
viewTab2controller = [[ViewTab2Controller alloc] initWithNibName:#"ViewTab2" bundle:nil];
viewTab2controller.title = #"Nearest Stop";
navigationTab2Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab2controller] autorelease];
[viewTab2controller release];
viewTab3controller = [[ViewTab3Controller alloc] initWithNibName:#"ViewTab3" bundle:nil];
viewTab3controller.title = #"Routes";
navigationTab3Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab3controller] autorelease];
[viewTab3controller release];
viewTab4controller = [[ViewTab4Controller alloc] initWithNibName:#"ViewTab4" bundle:nil];
viewTab4controller.title = #"Feedback";
navigationTab4Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab4controller] autorelease];
[viewTab4controller release];
//viewTab5controller = [[ViewTab5Controller alloc] initWithNibName:#"ViewTab5" bundle:nil];
//navigationTab5Controller = [[[UINavigationController alloc] initWithRootViewController:viewTab5controller] autorelease];
//[viewTab5controller release];
tabBarController.viewControllers = [NSArray arrayWithObjects:
navigationTab1Controller,
navigationTab2Controller,
navigationTab3Controller,
navigationTab4Controller,
//navigationTab5Controller,
I was given the code in the previous answer to add an image to a tabbaritem:
viewController.tabBarItem.image = [UIImage imageNamed:#"foo.png"];
However this doesn't specify the specific tabbbaritem.
How do I assign an image to each of these 4 tabs?
Thanks!
nil];
Do it like this for each View Controller that you'll be adding to your Tab Bar:
viewTab1controller = [[ViewTab1Controller alloc] initWithNibName:#"ViewTab1" bundle:nil];
viewTab1controller.title = #"Schedules";
navigationTab1Controller = [[UINavigationController alloc] initWithRootViewController:viewTab1controller];
navigationTab1Controller.tabBarItem.image = [UIImage imageNamed:#"Match.png"];