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.
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 push new StatusViewController(i created UITabbarViewController programmically in it) from rootviewController (UInavigation controller). Now, i want to click on logout button, it push rootviewcontroller but i used below code, it push rootviewcontroller fine but it still tabbar at the bottom.
This code to call rootviewcontroller:
LoginTab *loginView = [[LoginTab alloc] init];
[self.navigationController pushViewController:loginView animated:YES];
[loginView release];
And this code is created UItabbarcontroller in StatusViewController:
self.tab=[[UITabBarController alloc]init];
UploadTab *uploadview=[[UploadTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *uploadTabItem = [[[UINavigationController alloc] initWithRootViewController: uploadview] autorelease];
uploadview.title=#"Uploading";
uploadview.tabBarItem.image=[UIImage imageNamed:#"Uploading.png"];
self.title = #"FirstViewControllerTitle";
//SecondViewController
ConvertTab *convertView=[[ConvertTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *convertTabItem = [[[UINavigationController alloc] initWithRootViewController: convertView] autorelease];
convertView.title=#"Convert";
convertView.tabBarItem.image=[UIImage imageNamed:#"Convert.png"];
//ThirdViewController
CompletedTab *completedView=[[CompletedTab alloc]initWithNibName:nil bundle:nil];
UINavigationController *completedTabItem = [[[UINavigationController alloc] initWithRootViewController: completedView] autorelease];
completedView.title=#"Completed";
completedView.tabBarItem.image=[UIImage imageNamed:#"Completed.png"];
UIBarButtonItem * LogoutItem= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Logout.png"] style:UIBarButtonItemStylePlain target:self action:#selector(logout)];
self.navigationItem.rightBarButtonItem = LogoutItem;
self.tab.viewControllers=[NSArray arrayWithObjects:uploadTabItem,convertTabItem, completedTabItem, nil];
// [self.view insertSubview:self.tab.view belowSubview: uploadview.view];
[self presentModalViewController:self.tab animated:NO];
You can see this image :
Use popToRootViewControllerAnimated method instead of pushViewController:
[self.navigationController popToRootViewControllerAnimated:animated];
Your hierarchy does not seem correct. Your tab bar controller should be the root view controller. For each tab, you can have a navigation controller which has its own controllers to push and pop. That said, your tab bar will always be visible since that is the behavior that is expected when you have a tab bar based app. If you want to present a view which does not show a tab bar, you will need to present that view controller as a Modal view controller on top of your tab bar controller.
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];
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!
For an iPhone app how can I create a tab view programmatically, preferably in Objective-C?
It's quite simple to create a UITabBar via the UITabBarController. The following example should work within your AppDelegate class.
App Delegate Interface
Firstly, within the interface, we'll define our UITabBarController.
UITabBarController *tabBarController;
App Delegate Implementation
Then, within the implementation file's application:didFinishLaunchingWithOptions: method, we'll then initialise our tab bar controller.
// Initialise our tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
Next, you need to create the view controllers that you want to add to the tab bar controller. We'll need to add some information into these to set the tab's title/icon, but I'll come back to that at the end.
// Create your various view controllers
UIViewController *testVC = [[TestViewController alloc] init];
UIViewController *otherVC = [[OtherViewController alloc] init];
UIViewController *configVC = [[ConfigViewController alloc] init];
As the setViewControllers:animated: method requires an array of view controllers, we'll add our view controllers to an array and then release them. (As the NSarray will retain them.)
// Put them in an array
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil];
[testVC release];
[otherVC release];
[configVC release];
Then simply provide the UITabBarController with the array of view controllers and add it to our window.
// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers animated:NO];
// Put the tabBarController's view on the window.
[window addSubview:[tabBarController view]];
Finally, make sure you call [tabBarController release]; within your dealloc method.
View Controller Implementation
Inside each of your view controllers, you'll also want to set the title and icon for the tab within the init method as follows:
// Create our tab bar item
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:#"YOUR_IMAGE_NAME.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:#"YOUR TITLE"];
This is how we have to create tabbar programmatically
UINavigationController *BandNavigationController3;
AudienceSettingsViewController *audienceSettingsViewView =[[AudienceSettingsViewController alloc]initWithNibName:#"AudienceSettingsViewController" bundle:nil];
BandNavigationController3 = [[UINavigationController alloc]initWithRootViewController:audienceSettingsViewView];
BandNavigationController3.tabBarItem.title = #"Settings";
BandNavigationController3.tabBarItem.image = [UIImage imageNamed:#"settings.png"];
[BandNavigationController3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:4];
BandNavigationController3.navigationBar.hidden = YES;
[bandTabBarArray addObject:BandNavigationController3];
[BandNavigationController3 release];
[audienceSettingsViewView release];
[tabBarController setViewControllers:bandTabBarArray];
[bandTabBarArray release];