Disable/hide uitabbaritem on an uitabbar - iphone

I found this code here that i think will do the job.
`/* suppose we have a UITabBar *myBar, and an int index idx */
NSMutableArray modifyMe = [[myBar items] mutableCopy];
[modifyMe removeObjectAtIndex:idx];
NSArray *newItems = [[NSArray alloc] initWithArray:modifyMe];
[myBar setItems:newItems animated:true];`
The question is, where should i write this code in order to work?
I tried putting it in viewDidLoad of the UITabBarController but it didn't work.

If you're modifying the tabbar items from the UITabbarController, you can't use setItems:animated:. From the docs:
In iOS 3.0 and later, you should not attempt to use the methods and properties of this class to modify the tab bar when it is associated with a tab bar controller object. Modifying the tab bar in this way results in the throwing of an exception. Instead, any modifications to the tab bar or its items must occur through the tab bar controller interface.
Instead, swap out the viewControllers property of your UITabbarController, removing the UIViewController that corresponds to the tabbar item you want removed. For example, if you want to remove the 2nd tabbar item:
NSMutableArray *newViewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
[newViewControllers removeObjectAtIndex:1];
[self setViewControllers:newViewControllers];

Related

how to change UIViewController in a Tab of TabBarController

I have an iPhone app with TabBar and tabs. Each tab is loaded with UIViewControllers, what I want for a particular tab is to change UIViewController associated with tab. When I call PresentViewController it changes UIViewController but also hides the TabBar which i dont want.
Can anybody please explain what needs to be done ?
Thanks
UITabBarController keeps a collection of it's view controllers in a property aptly named viewControllers. You can modify this at runtime. There are side effects that probably are fine for your app, but read the docs to be sure.
A convenience method (and illustration of how to modify that immutable array) would look like this:
- (void)replaceTabBarViewControllerAtIndex:(NSUInteger)index with:(UIViewController *)newVC {
NSMutableArray *newVCs = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
if (index < newVCs.count) {
newVCs[index] = newVC;
self.tabBarController.viewControllers = [NSArray arrayWithArray:newVCs];
}
}
Call this with the new vc instead of presenting it.

Change ViewController for a TabBarItem

Is it possible to change/replace ViewContoller (and View) for one of the Tabs in a UITabBarController?
I would like to switch between 3 different ViewControllers in any order from a specific Tab (that´s why NavigationController is not possible).
They are set (in mass) using setViewControllers:animated: so you could do something like this.
// Assume tabController is the tab controller
// and newVC is the controller you want to be the new view controller at index 0
NSMutableArray *newControllers = [NSMutableArray arrayWithArray:tabController.viewControllers];
[newControllers replaceObjectAtIndex:0 withObject:newVC];
[tabController setViewControllers:newControllers animated:YES];
Hope that helps.

How to remove a UITabBarItem

I have a nib with four UIViewControllers each with a UITabBarItem. At runtime, based on user options, I need to show or hide one of the UITabBarItems. I cannot figure out how to remove the UITabBatItem.
Does anyone know how to do this?
Thanks
Christian's code is close. It should be:
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:myTabBarController.viewControllers];
[viewControllers removeObjectAtIndex:indexToRemove];
[myTabBarController setViewControllers:viewControllers];
To remove one, you can just get the viewControllers from the TabBar and put them in an NSMutableArray. Then remove the index you want removed and then set the viewControllers property to this new array of view controllers using
setViewControllers:animated:
Hoep this helps.
Get the tab bar controller's view controllers, remove the one you want to 'hide' and then set the tab bar controller's viewControllers array to this new array.
NSMutableArray *viewControllers = [myTabBarController viewControllers];
[viewControllers removeObjectAtIndex:indexToRemove];
[myTabBarController setViewControllers:viewControllers animated:YES];

Is it possible in a UITabBarController to have one tab in a fixed position and able reorder the other tabs?

I would like for one tab in a UITabBarController to have a fixed position and the user can reorganize the other tabs however they like. I have the UITabBarController > UINavigationController > UITableViewController setup.
Is this possible?
UPDATE:
The following code, in applicationDidFinishLaunching, is what I'm using and it doesn't work. I'm trying to get a reference to the SavedTableViewController, I think that's the problem. Any ideas?
NSMutableArray *customizableVCs = [NSMutableArray arrayWithArray:tabBarController.customizableViewControllers];
for (id controller in tabBarController.customizableViewControllers){
if ([controller isKindOfClass:[SavedTableViewController class]]){
NSLog(#"Removing Object");
[customizableVCs removeObject:controller];
}
}
tabBarController.customizableViewControllers = customizableVCs;
UPDATE 2:
This is the code that worked for me. I had to put the snippet below adding the tabBarController to the window.
[window addSubview:tabBarController.view];
[self setTabOrderIfSaved];
NSMutableArray *customizableViewControllers = [NSMutableArray arrayWithArray:tabBarController.customizableViewControllers];
[customizableViewControllers removeObject:savedNavigationController];
tabBarController.customizableViewControllers = customizableViewControllers;
Remove the view controller corresponding to that tab from the customizableViewControllers property of your tab bar controller. Assuming that you want fixedViewController to remain fixed:
NSMutableArray *customizableViewControllers = [NSMutableArray arrayWithArray:tabBarController.customizableViewControllers];
[customizableViewControllers removeObject:fixedViewController];
tabBarController.customizableViewControllers = customizableViewControllers;
Because customizableViewControllers is reinitialized when the viewControllers property is changed, you should do this after the rest of your tab bar controller’s state has been set up.

UINavigationController, UITableView and not showing the next view

I am having a problem with a table and showing another view when the user touches a table cell. I am using a UITabBarController for the main view to show different views. I then use a UINavigationController when the user selects a UITableView view to display the next view from the table cell selection.
My problem is this: when they select the table cell, the cell is highlighted and the next view does not appear. I am using IB. All of the examples I have found are doing this the same way I am, except the examples work!
I have the controllers loaded in a NSMutableArray with a Dictionary. I also tried this code by directly creating a view controller rather than using the array item. Same results....
I checked the target controller (Calc1ViewController) to make sure there was nothing wrong with the controller. When showing it directly, the controller (Calc1ViewController) displays correctly.
Here is some code.....
The initialization of the view controller in the dictionary:
Calc1ViewController *calc1ViewController = [[Calc1ViewController alloc] init];
[menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(#"SSC - page 1",#""), #"title",
calc1ViewController, #"viewController",
nil]];
[calc1ViewController release];
I also tried this without using the dictionary -- creating a view controller in place of the [[menuList objectAtIndex:...]; line -- by creating the view controller like this:
Calc1ViewController *targetViewController = [[Calc1ViewController alloc] init];
// the table's selection has changed, switch to that item's UIViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *targetViewController = [[menuList objectAtIndex: indexPath.row] objectForKey:#"viewController"];
[[self navigationController] pushViewController:targetViewController animated:YES];
targetViewController = nil;
}
The navigation controller is defined in the AppDelegate and connected to the RootViewController through IB.
When I select a table row, it shows in the debugger that the pushViewController code is executed, but the next view does not show. Here is an example:
alt text http://thecoolestcompany.com/ssc-table-view.jpg
Any ideas?
Finally figured it out......
I had the UINavigationController and the UITabBarController on the same level. After moving the Navigation Controller as a child to the tab bar controller, everything worked. You may add as many navigation controllers to a tab bar as you would like. In IB is is a little confusing. You need to drag the NavigationController into the TabBar to as it as a new Tab Bar item.
Theres not much to go by with t he code you have provided. Only thing i can think of without looking at more code is that perhaps you have not initialized your view controller (the one in your dictionary) correctly, or at all, or the dictionary you are accesing there is not the correct one...
What is the value of [self navigationController] when pushViewController is called. Is it nil?
What is the value of targetViewController when it is passed to pushViewController. Is it nil?