I have experience in C and C++, but near zero experience in Objective C, or Xcode4.
Im looking to create an app with a Tab bar, Navigation Bar, and Table Views. Based on the knowledge i have i assume i start from the top and drill down to the root?
First
Create myTableViewController class that will dynamically create tableview content and push its created view onto the navigation controller.
Then...
Create myNavController class that holds myTableViewController. with a method that creates a new item for myTableViewController.
Then...
Create the Tab Bar Controller that has the above as one of its tabs in an array along with some other tabs, set the tab bar controller as the root controller and display it to the window.
Is this the right direction to be thinking? Or am i horribly off course?
I have an app with these same requirements. It's got a UITabBar, and in the different tabs each UITableViewController has a UINavigationController navigation bar at the top.
Here's how my App Delegate handles this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Create the UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the view controllers for our tabs
UITableViewController *vc1 = [[UITableViewController alloc] init];
UITableViewController *vc2 = [[UITableViewController alloc] init];
UITableViewController *vc3 = [[UITableViewController alloc] init];
UITableViewController *vc4 = [[UITableViewController alloc] init];
UITableViewController *vc5 = [[UITableViewController alloc] init];
//Create the Navigation Controllers for these views
UINavigationController *nc1 = [[[UINavigationController alloc]
initWithRootViewController:vc1] autorelease];
UINavigationController *nc2 = [[[UINavigationController alloc]
initWithRootViewController:vc2] autorelease];
UINavigationController *nc3 = [[[UINavigationController alloc]
initWithRootViewController:vc3] autorelease];
UINavigationController *nc4 = [[[UINavigationController alloc]
initWithRootViewController:vc4] autorelease];
UINavigationController *nc5 = [[[UINavigationController alloc]
initWithRootViewController:vc5] autorelease];
//Make an array containing the view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:nc1, nc2, nc3, nc4, nc5, nil];
//The NSArray has retained these controllers, we can now release them.
[vc1 release];
[vc2 release];
[vc3 release];
[vc4 release];
[vc5 release];
[nc1 release];
[nc2 release];
[nc3 release];
[nc4 release];
[nc5 release];
//Assign the view controllers to the tab bar.
[tabBarController setViewControllers:viewControllers];
//Set tabBarController as rootViewController of window
[self.window setRootViewController:tabBarController];
//The window retains tabBarController, we can release our reference
[tabBarController release];
[self.window makeKeyAndVisible];
return YES;
}
Enjoy!
Related
I started using UITabBarController and it is great.
The thing is I have a few views that aren't accessed from the UITabBar
(either presented modal programatically or we want to have a button on the top to jump to them)
The thing is that I want to retain the Tab bar visible in these views.
From my understanding mixing presentViewController and UITabBarController is problematic.
How can I do that? Can I have "hidden" tab bar elements I can reference programatically?
Just to clarify with an example:
Views A,B,C,D are in the tab bar - via the storyboard - everything is peachy.
I NEED to have views E and F clickable from the top navigation (please don't suggest a sliding TabBar or a multiple line UITabBar).
I could just jump to E and F but I want the UITabBar to still be visible so the user can jump from E to A for example.
Just use the good old UINavigationController for every tab and just use [self.navigationController pushViewController:A animated:YES];
That's how the setup looks in code:
SGTabBarViewController *rootVC = [[SGTabBarViewController alloc] init];
SGFirstTabViewController *firstVC = [[SGFirstTabViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
SGSecondTabViewController *secondVC = [[SGSecondTabViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
SGThirdTabViewController *thirdVC = [[SGThirdTabViewController alloc] init];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdVC];
SGForuthTabViewController *fourhtVC = [[SGForuthTabViewController alloc] init];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:fourhtVC];
rootVC.viewControllers = #[navController1, navController2, navController3, navController4];
self.window.rootViewController = rootVC;
[self.window makeKeyAndVisible];
If you want your UITabBar visible on every VC you push just use hidesBottomBarWhenPushed = NO; on it.
However, there is no way to have UITabBar visible on views presented modally.
I have 5 views in my app and I'm appDelegate by setting them in the following way:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller1, navigationcontroller7, navigationcontroller5, navigationcontroller4, navigationcontroller6, navigationcontroller2, nil];
self.window.rootViewController = tabBarController;
All of them come with a NavigationController and tabbarcontroller, But I needed to split the screen into two parts, in this case the screen would be divided would be navigationcontroller2, as you can see below:
VendaViewController *venda_viewcontroller = [[VendaViewController alloc] init];
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] init];
[navigationcontroller2 pushViewController:venda_viewcontroller animated:YES];
Hence I tried the following way:
VendaViewController *venda_viewcontroller = [[VendaViewController alloc] init];
VendaDetailViewController *vendaDetail_viewcontroller = [[VendaDetailViewController alloc] init];
UISplitViewController *splitVC = [[UISplitViewController alloc] init];
[splitVC setViewControllers:[NSArray arrayWithObjects:venda_viewcontroller,vendaDetail_viewcontroller,nil]];
UINavigationController *navigationcontroller2 = [[UINavigationController alloc] init];
[navigationcontroller2 pushViewController:splitVC animated:YES];
But not work in this code, but in documentation of UISplitViewController is writing the following message:
"you must always install the view from a UISplitViewController object
as the root view of your application’s window. [...] Split view
controllers cannot be presented modally."
So...If I like to put a splitViewController in my view controller, I'll have to put splitViewController in all of my views controllers? Or have another solution to this?
You can use a UISplitViewController only as the root view controller of your app. In your case you can implement your custom container view controller with functionality similar to the split (two subview controller inside the main). Follow this link for details.
My IOS app has a login sequence that cannot be modified, once the sequence is complete I do the following in the app delegate
- (UIViewController*)newRootViewController {
NViewController *nView = [[NViewController alloc]
initWithNibName:#"NViewController"
bundle:nil];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:nView];
[nView release];
return navVC;
}
Once in nView is it possible to add a tab bar controller or how can I replace nView with a tab bar controller.
You can create a UITabBarController then add your NViewController and other controllers to the tab bar.
NViewController *nView = [[NViewController alloc]
initWithNibName:#"NViewController"
bundle:nil];
//Create my tab bar
UITabBarController* myTabController = [[UITabBarController alloc]init];
//Add my tabs
NSArray* tabs = [[NSArray alloc]initWithObjects:nView, nil];
[myTabController setViewControllers:tabs];
How do you add the Navigation items on the popover (left pane) of the iPad's splitview? This is where the mailboxes/inbox/drafts part of the navigation is in the iPad's builtin email application.
Make the UISplitViewController’s “master” pane a UINavigationController, then just push UIViewControllers on it that have navigationItems.
Here’s a sample setup:
UIViewController *masterController = [[MyCustomMasterController alloc] init…];
[[masterController navigationItem] setTitle:#"Root"];
UINavigationController *navController =
[[UINavigationController alloc] initWithRootController:masterController];
UIViewController *detailController [[MyCustomDetailController alloc] init…];
UISplitViewController *splitView = [[UISplitViewController alloc] init];
[splitView setViewControllers:[NSArray arrayWithObjects:navController,
detailController,
nil]];
And then later on:
UIViewController *subController = [[MyCustomSubController alloc] init…];
[[masterController navigationController] pushViewController:subController
animated:YES];
Pushing a new UIViewController to the UINavigationController’s stack will cause a back button titled “Root” to appear for the MyCustomMasterController view.
I had a navigation controller based application. And I decided to use tab bars in my application.
When the user presses at a certain tab bar item I want to display a certain view controller - and I want programmatically in my code choose which one to display.
I tried to add in the Interface Builder a navigation controller into my tab bar, but viewWillAppear of its view controller is not being called.
How can I implement this feature?
I don't know if it's the "right way", but here's how I usually do this with three tabs.
- (void)initControls {
// Create the window.
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
// Create Tab Bar.
tabCon = [[UITabBarController alloc] init];
// Local array variable that holds the viewcontrollers.
// Capacity corresponds to the number of VC's
NSMutableArray *localVCArray = [[NSMutableArray alloc] initWithCapacity:3];
MyFirstViewController *oneViewController = [[MyFirstViewController alloc] init];
UINavigationController *oneNavCon = [[UINavigationController alloc] initWithRootViewController:oneViewController];
[localVCArray addObject:oneNavCon];
[oneViewController release];
[oneNavCon release];
MySecondViewController *twoViewController = [[MySecondViewController alloc] init];
UINavigationController *twoNavCon = [[UINavigationController alloc] initWithRootViewController:twoViewController];
[localVCArray addObject:twoNavCon];
[twoViewController release];
[twoNavCon release];
MyThirdViewController *threeViewController = [[MyThirdViewController alloc] init];
UINavigationController *threeNavCon = [[UINavigationController alloc] initWithRootViewController:threeViewController];
[localVCArray addObject:threeNavCon];
[threeViewController release];
[threeNavCon release];
// Set the tab bars array of view controllers to the localVCArray
[[self tabCon] setViewControllers:localVCArray animated:YES];
// Release the localVCArray, all of its contents are now retained by tabCon.
[localVCArray release];
// Add controls to window and show.
[window addSubview:[tabCon view]];
[window makeKeyAndVisible];
}
In the init method each viewController you can do something like:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
To set the icon used in the tab bar, the title in the tab bar, and the title of you navigation item.