add/hide UITabBarItem - objective-c

I have a UITabBarController and would like to add a tab, with the ability to make it disappear. I added 2 tabs with XCode. You can add a third tab by program?
I know a command like:
[self setViewControllers: [NSArray arrayWithObjects: x1, nil] animated: NO];
You can retrieve the array that uses XCode to add a third view?
thanks
I can not load the view with the code. I created a view controller from the storyboard, when I try to load it from code I get a black screen, use this code:
ViewControllerA *x1 = [[ViewControllerA alloc] init];
[self setViewControllers:[NSArray arrayWithObjects: x1, nil] animated:NO];

Yes, if you use [UITabViewController setViewControllers: animated:] you can add in an array containing the two previous view controllers plus the new third one.
For example, you'd probably want to do it something like this:
// assuming you've set an IBOutlet to your tab bar controller
NSArray * currentSetOfViewControllers = [appTabBarController viewControllers];
if((currentSetOfViewControllers == NULL) || [currentSetOfViewControllers count] == 0))
{
NSLog( #"I don't see any view controllers; is my tab bar controller outlet set properly?")
} else {
NSMutableArray newSetOfViewControllers = [[NSMutableArray alloc] initWithArray: currentSetOfViewControllers];
if(newSetOfViewControllers)
{
ViewControllerA *x1 = [[ViewControllerA alloc] init];
if(x1)
{
[newSetOfViewControllers addObject: x1];
[appTabBarController setViewControllers: newSetOfViewControllers];
// release our alloc'd mutable array only if you do not have ARC turned on
[newSetOfViewControllers release];
}
}
}
You'd probably also want to give your new view controller an associated tab bar item with a title and an image. Check out [UITabBarItem initWithTitle: image: tag:].
I've linked the Apple documentation for you which I hope would help!

create a custom tab bar in delegate so create a view just like a tab bar and set 1 image view on view and 3 button (1 button set hidden) add this view in tab bar controller after that when u require 3rd button enable third button

Related

two ViewControllers for one tab in a tabBarController

I have a tabBarController (that i created using code without Interface Builder):
self.tabBarController = [[[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstController, secondController, thirdController, nil];
when i'm in the second tab (secondController) and i press a button:
UIButton *button1;
I want to pass to another view (seconddController for example):
- (void)actio:(UIButton *)button1
{
seconddController *scdd = [[seconddController alloc] init ];
[self presentModalViewController:scdd animated:YES];
}
But the problem is that i want to stay on the second tab and not that the view occupies all the space, so how to add that seconddCntroller to the second tab in lieu of secondController?
Thank you
If I understand you correctly, you need to have secondController managed by a navigation controller so that the structure is:
self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstController, secondNav, thirdController, nil];
Then you can use pushViewController:animated: to show scdd over secondController.

How to not push a segue into the Nav Controller Stack?

I would like to know what kind of trick to use for "not pushing" a view controller into the navigation controller stack (iOS)
I have this :
If user is not logged, show view A then show B
If user is logged, show B
As I am using the storyboard, I used a performSegue if the user is logged so he goes directly to B. But with this method, the Navigation Controller gets a push of view A in the stack.
I was thinking of poping out a level of the stack in some void (but I don't know how to do this).
I was also thinking of not pushing the view into the nav controller stack (but I don't know how to do this).
Thanks
Update :
I tried this :
//The view B
TabBarMain* mainViewController = [[TabBarMain alloc] init];
//If already logged in
if([username length] == 0)
{
NSArray *viewControllers = [NSArray arrayWithObject:mainViewController];
[self.navigationController setViewControllers:viewControllers animated:NO];
}
The problem of this code is that it shows me a black screen (doesn't crash). It seems that I need to init something and I have nothing in my TabBarMain.m, I don't know what to write in there. This TabBarMain is linked to the Tab Bar Controller of the Storyboard.
Is there no other way ?
Try this on for size in your rootViewController's viewDidLoad.
- (void)viewDidLoad
{
NSArray *viewControllers
if (logged) {
NSArray *viewControllers = [NSArray arrayWithObject:viewControllerB];
} else {
NSArray *viewControllers = [NSArray arrayWithObject:viewControllerA];
}
[self.navigationController setViewControllers:viewControllers animated:NO];
}
Since your viewController is linked in Storyboard and not instantiated in code you need to instantiate it from the storyboard not your empty code. Make sure the identifier matches the identifier for your ViewController in your storyboard.
TabBarMain *mainViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:#"tabBarMain"];

Tab Bar controller won't respond to viewControllers in the "more" section

I have a tab bar view controller that is controlling just about everything in my app. I have manually coded it within my appDelegate and it displays all the views just fine but does not respond to certain things when it comes to all the view controllers that are in the "more" tab section (this is automatically created if you 6 or more view controllers). I cannot even add icons to be displayed beside the view controller name... They only show up in the main tab bar section and not in the table list.
Does anyone know why this is happening only in this part of my app? Here are examples of code that work in OTHER view controllers but NOT in the "more" section:
nav7.navigationBar.tintColor = [UIColor colorWithRed:117/255 green:4/255 blue:32/255 alpha:1]; //(in appDelegate.m)
UIImage *tab6 = [UIImage imageNamed:#"130-dice.png"];
NSArray *tabBarImages = [[[NSArray alloc] initWithObjects:tab1, tab2, tab3, tab4, tab5,tab6, nil]autorelease];
NSInteger tabBarItemCounter;
for (tabBarItemCounter = 0; tabBarItemCounter < [tabBarItems count]; tabBarItemCounter++)
{
UITabBarItem *tabBarItem = [tabBarItems objectAtIndex:tabBarItemCounter];
tabBarItem.image = [tabBarImages objectAtIndex:tabBarItemCounter];
}
Thank you!
If you properly set a title and image on the tab bar item of a view controller it should work just fine, like so:
- (id)init {
self = [super init];
if (self) {
self.tabBarItem.title = #"Videos";
self.tabBarItem.image = [UIImage imageNamed:#"video"];
}
return self;
}

Adding additional UITabbarItem to UITabbarController

I am wondering if there is a way to add an additional UITabBarItem to my exisiting UITabBarController. It doesn't need to be in runtime.
All I want to do is when hitting this button I want to presentModalViewController: over my actually visible ViewController, which should either be the TabBarController or its controllers.
Hopefully this is clear enough, if not, feel free to ask.
As a result of my research you cannot add a UITabBarItem to a UITabBar that is managed by a UITabBarController.
Since you maybe have added your UITabBarItem by adding a list of view controller, this is also the way of your choice to add further custom UITabBarItems, as i will show now:
Pre-Conditions:
As i mentioned before, you maybe have added your UITabBarItems by adding a list of view controller:
tabbarController = [[UITabBarController alloc] init]; // tabbarController has to be defined in your header file
FirstViewController *vc1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
vc1.tabBarItem.title = #"First View Controller"; // Let the controller manage the UITabBarItem
SecondViewController *vc2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:[NSBundle mainBundle]];
vc2.tabBarItem.title = #"Second View Controller";
[tabbarController setViewControllers:[NSArray arrayWithObjects: vc1, vc2, nil]];
tabbarController.delegate = self; // do not forget to delegate events to our appdelegate
Adding custom UITabBarItems:
Now since you know how to add UITabBarItems through adding view controller, you can also use the same way to add custom UITabBarItems:
UIViewController *tmpController = [[UIViewController alloc] init];
tmpController.tabBarItem.title = #"Custom TabBar Item";
// You could also add your custom image:
// tmpController.tabBarItem.image = [UIImage alloc];
// Define a custom tag (integers or enums only), so you can identify when it gets tapped:
tmpController.tabBarItem.tag = 1;
Modify the line above:
[tabbarController setViewControllers:[NSArray arrayWithObjects: vc1, vc2, tmpController, nil]];
[tmpController release]; // do not forget to release the tmpController after adding to the list
All fine, now you have your custom button in your TabBar.
What about handling events of this custom UITabBarItem?
Its easy, look:
Add the UITabBarControllerDelegate to your AppDelegate class (or the class which is holding the tabbarController).
#interface YourAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { }
Fit the protocol definition by adding this function:
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
UITabBarItem *item = [theTabBarController.tabBar.items objectAtIndex:indexOfTab];
NSLog(#"Tab index = %u (%u), itemtag: %d", indexOfTab, item.tag);
switch (item.tag) {
case 1:
// Do your stuff
break;
default:
break;
}
}
Now you have all you need to create and handle custom UITabBarItems.
Hope this helps.
Have fun....
Access the tabBar - property of your UITabBarController (reference), grab the elements array with the items - property (reference), add a new UITabBarItem to this array and use the tabBar's setItems:animated: - method to update your tab bar. Add an action to this tab bar to display the modal view controller.

How combine TabBar + Navigation with XCode

I'm triying to combine a TabBar + Navigation app.
I have 5 tab bars, 4 are listing of stuff and drill down to details views.
I try to follow this tutorial:
http://www.iphonedevforums.com/forum/iphone-sdk-development/124-view-controller-problem.html
But always get a blank view.
This is what I do, with a clean project:
I start with a TabBar template app.
I put 5 tab bar buttons.
I create a controller like:
#interface FirstViewController : UINavigationController {
}
I put the main window.xib on tree mode & change the selected first view to FirstViewController
I select the TabBar Controller in Interface builder, go to TabBar Attributes & change the class to navigation controler.
Select the fist view & put the nib name "SecondView"
In response, I get a blank screen.
I must add that I wanna navigate from the details views, no from the main windows.
i.e in the main window tab bar 1 is the list of people. I select a person then wanna navigate to the detail window.
First, you never want to subclass UINavigationController or UITabBarController.
Second, I did not quite get what you did, but a correct sequence to create a navigation controller inside a tab bar controller is something like this:
// in MyAppDelegate.h
UINavigationController *nc1, *nc2;
FirstTabRootViewController *vc1;
SecondTabRootViewController *vc2;
UITabBarController *tbc;
// in MyAppDelegate.m
nc1 = [[UINavigationController alloc] init];
vc1 = [[FirstTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc1.tabBarItem.title = #"Tab 1";
vc1.tabBarItem.image = [UIImage imageNamed:#"tab1.png"];
vc1.navigationItem.title = "Tab 1 Data";
nc1.viewControllers = [NSArray arrayWithObjects:vc1, nil];
nc2 = [[UINavigationController alloc] init];
vc2 = [[SecondTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc2.tabBarItem.title = #"Tab 2";
vc2.tabBarItem.image = [UIImage imageNamed:#"tab2.png"];
vc2.navigationItem.title = "Tab 2 Data";
nc2.viewControllers = [NSArray arrayWithObjects:vc2, nil];
tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nc1, nc2, nil];
Note that it's your view controller that controls the text/icon in the tab bar and in the navigation bar. Create a UINavigationController instance for each of your tabs; UINavigationController contains a stack of view controllers (see viewControllers property) which should contain at least one item — your root controller for that tab. Also create an UITabBarController to manage the tabs.
Of course, you can (and probably should) use interface builder instead of code to instantiate the mentioned classes and set the properties. But it's important that you understand what happens behind the scenes; interface builder is nothing more than a convenient way to instantiate and set up objects.
Hope this is helpful; please refine your question if it's not.
Still getting the blank screen On Starting the application after implementing the above code. Where i 'm writing it wrong.
nc1 = [[UINavigationController alloc] init];
nc2 = [[UINavigationController alloc] init];
vc1 = [[FirstRootViewController alloc]initWithNibName:#"FirstRootViewController" bundle:nil];
vc1.tabBarItem.title = #"Item 1";
vc1.tabBarItem.image= [UIImage imageNamed:#"home.png"];
vc1.navigationItem.title = #"Tab1 Data";
nc1.viewControllers = [NSArray arrayWithObjects:vc1,nil];
vc2 = [[SecondRootViewController alloc]initWithNibName:#"SecondRootViewController" bundle:nil];
vc2.tabBarItem.title = #"Item 2";
vc2.tabBarItem.image= [UIImage imageNamed:#"home.png"];
vc2.navigationItem.title = #"Tab2 Data";
nc2.viewControllers = [NSArray arrayWithObjects:vc2,nil];
tbc = [[UITabBarController alloc]init];
tbc.viewControllers = [NSArray arrayWithObjects:nc1,nc2,nil];
[window addSubview:tbc.view];
[window makeKeyAndVisible];
Here is an tutorial that I was able to get working.
I also read the official SDK documentation on the topic: Combining Tab Bar and Navigation Controllers. Since I'm still learning, the tutorial helped me more than the docs.
NOTE: in the tutorial, i don't think you need to subclass UINavigationController, and I'm experimenting with this idea now.
I tried to create an iphone application with UITabBarController and some UINavigationController inside it and faced the same problems as "mamcx". With your example-code i get it to run :) Thanks a lot.
Here is how it works for me.
// YOURS
fourthNavigation = [[UINavigationController alloc ] init ];
fourthViewController = [[[FourthTabRootController alloc] initWithNibName:#"FourthView" bundle:nil] autorelease];
fourthNavigation.tabBarItem.title = #"YOURS";
fourthNavigation.viewControllers = [NSArray arrayWithObjects:fourthViewController, nil];
// Add self-defined UIViewControllers to the tab bar
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavigation,secondNavigation, thirdNavigation, fourthNavigation, nil];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
The other UINavigationControllers "firstNavigation, ... " are created the same way.
I load the view elements from nib-files that are connected to my subclassed UIViewController classes. You dont need to add a NavigationBar in the IB to your view, cause the UINavigationController has allready one. So you only need to set the title in "initWithNibName"
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = #"YOURS";
}
return self;
}
I hope that helps.
Check out "Adding a Navigation Controller to a Tab Bar Interface" under View Controller Catalog for iOS which take you step by step into how exactly this can be achieved