two ViewControllers for one tab in a tabBarController - objective-c

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.

Related

PresentModalViewController not showing TabBar

I'm trying to go from one xib to another and I'm using TabBar.
When I move from xib to xib with PresentModalViewController I lose the TabBar.
When I use this way, it fail (like force close in android):
FirstViewController *fvc = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
[fvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
fvc.userSelectedLatitude = saveLatitude;
fvc.userSelectedLongitude = saveLongtitude;
UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:fvc bundle:nil];
[self presentModalViewController:tabControl animated:NO];
When I use:
UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:#"FirstViewController" bundle:nil];
I get black screen with TabBar.
Since it all fail I guess it is not the right way.
So, what is the right way to do it?
The code above crashes because you are trying to pass a view controller instead of an NSString object in the initWithNibName:bundle: method.
The way to do it depends of what you really want to do. Do you want to present the xib in a modalViewController with or without a tabBar?, or simply present the view controller modally?.
UPDATE:
Fine, you have to create first your view controllers associated to each tabBar button (like you have been doing until now), after, add these view controllers to your tabBar and then present the tabBarController modally. Like this:
FirstViewController *fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
fvc.userSelectedLatitude = saveLatitude;
fvc.userSelectedLongitude = saveLongtitude;
UITabBarController *tabControl = [[UITabBarController alloc] init];
[tabControl setViewControllers:[NSArray arrayWithObjects:fvc, nil]];
[tabControl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:tabControl animated:NO];
I think this code should work. So, try it and tell us if something goes wrong.
With UITabBarController, there is no need to manually present viewControllers or call code to switch the views. This is handled for you.
All you need to do is set the viewControllers property of the UITabBarController. Like so:
[tabBarController setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];

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.

Add button to my View "Header/Title" bar

In my AppDelegate, I create a UITabBarController and UINavigationController dynamically. I then add 3 views to it.
// Prepare the tab bar controller
tabBarController = [[UITabBarController alloc] init];
// Switch controller
UserSettingsController *settingsController = [[UserSettingsController alloc] init];
// Switches controller
SwitchesController *switchesController = [[SwitchesController alloc] init];
// Help controller
HelpController *helpController = [[HelpController alloc] init];
NSArray *controllers = [NSArray arrayWithObjects: switchesController, settingsController, helpController, nil];
tabBarController.viewControllers = controllers;
if (self.navigationController == nil) {
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tabBarController];
}
[window addSubview:navigationController.view];
When I initially did this, I noticed that I now have a header/title bar at the top of my page. It's blank and really just takes up space. I'd like to utilize it though and add a "refresh" button to one of my views.
In the view I'm interested in, I attempted the following:
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonItemStylePlain target:self action:#selector(refreshSwitches:)];
self.navigationItem.rightBarButtonItem = refresh;
[refresh release];
}
-(void)refresh{
...
}
No button showed up, so I'm either way off or I'm missing something.
Any suggestions?
Thanks
I believe the problem is that your view controllers that you've added to the tabbar controller are not encapsulated by navigation controllers. Although your UITabBarController has a navigation controller, this does not implicitly give all of its tabs navigation controllers (and without a nav controller, a view controller's navigationItem won't do anything.) To remedy this problem I'd suggest encapsulating your view controllers with navigation controller upon initialization:
ex:
UserSettingsController *settingsController = [[UserSettingsController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:settingsController];
[settingsController release];
// etc...
NSArray *controllers = [NSArray arrayWithObjects:navController, ..., ..., nil];
tabBarController.viewControllers = controllers;
// Release your nav controllers, other cleanup
EDIT
Okay, I think I have a better idea of your setup now. I was able to get the following to work:
self.tabBarController.navigationItem.rightBarButtonItem = myButton;
An important caveat of this is that the button will persist on all of your tabbarcontroller's views unless you explicitly remove it. This may prove annoying/inelegant to maintain, but I am not sure of another solution if the view you want to have the button is not directly associated to a nav controller itself.

UITableView+UINavigationBar+UITabbar in windows based application template using interface Builder?

i want a view with UITabBAr at The bottom with some buttons+Navigation controller on each view and a tableview inside the view using interface builder and windows basaed application template. i want a step by step guide to do this
This is very simple. Just create your appdelegate with this function:
- (void)applicationDidFinishLaunching {
TableSubclass *tvc = [[TableSubclass alloc] init]; //subclass of UITableView you declare
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:tvc];
navBar.tabBarItem.title = #"Foo";
NSArray *tabViewControllerArray = [NSArray arrayWithObjects:navBar, nil];
self.tabBarController.viewControllers = tabViewControllerArray;
self.tabBarController.delegate = self;
[tvc release];
}
Now, just create your UITableView subclass and you are all set.
If you want multiple views, and not just one table, you can just declare more view controllers and add them to the tabViewControllerArray.

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