XCode TabBar app - multiple views? - iphone

I have been working to create an app with tabs at the bottom, but with the ability to switch views with my own links on the nib's that the tabBar app displays.
I have been trying to find an example of this but xcode 4.2 (whether on ios5.0 or earlier) does not create a mainwindow.xib, so i have had to find a way to create this programmataiically as follows:
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "ViewListViewController.h"
#import "ShareViewController.h"
#import "FriendsViewController.h"
#import "SettingsViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
UIViewController *viewListViewController = [[ViewListViewController alloc] initWithNibName:#"ViewListViewController" bundle:nil];
UIViewController *shareViewController = [[ShareViewController alloc] initWithNibName:#"ShareViewController" bundle:nil];
UIViewController *friendsViewController = [[FriendsViewController alloc] initWithNibName:#"FriendsViewController" bundle:nil];
UIViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeViewController, viewListViewController, shareViewController, friendsViewController, settingsViewController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
This code in my AppDelegate.m works fine and i can switch between the tabs perfectly. The issue I am having is that I will be adding graphics to the pages and using them as buttons to switch between views for adding/editing/deleting data from a database (separate issue I will have to learn to do next).
Most examples i have found start the app with either a tabBar, or a uiNavigation with UIViews to switch between. I have yet to find an example that will clearly show how to do what I am looking to do. Is my approach wrong to create the tabBar application as my base template for this, or can this be done? Each tab may have multiple views that perform functions, but I need to be able to create and switch between them within the tabBar framework.
thanks in advance,
Silver Tiger
-UPDATE:-
AFter reading up a bit I am now thinking that aUINavigationController is not what I am looking for. What I need is a way for a custom button that I create to allow me to switch views, as the buttons I will be using in my content will be either graphics or transparent on top of a graphic background. Is it possible to use custom buttons to push new views while still keeping the tabBar visible at all times? My structure would be as below
Home
Add
Edit
delete
ViewList - Single page to view list
Share - Single page to send emails / invites
Friends
Add
Edit
Delete
Settings - Single page for app settings/preferences
so essentially i'd only really have 2 of the tabs where i would be pushing new views but the UINavigationController doesn't seem to fit the method i am using since i am not using a single bar up top to switch views.

Both UINavigationController and UITabBarController are controllers of view controllers. In this function, they work the same way. But there are some differences. Use this as a guide:
UINavigationController: Here you usually have a navigation bar at the top. You "push" other view controllers onto a "stack" and the navigation controller takes care of all the buttons on top to go back and forth as well as the animations, etc. Often, but not always, this is implemented with table views.
UITabBarController: Here you have the tab bar at the bottom at all times, so this should only make sense if there are things the user would typically have to access regardless of where she is in the view hierarchy. Look at the Alarm Clock app for a typical example.
Combination: If you want to combine the two, you start with the tab bar controller. You can then assign separate navigation controllers to one or more tab bar items. The navigation controllers in turn manage their view controllers. Make sure your UI does not get too cluttered with navigation controls at the top and bottom of the screen. Look at the iTunes App for a typical example.
BTW, you can always make these controls disappear, if one view needs more space (e.g. in the iPod app the tab bar gets replaced by a toolbar when going to "Now Playing").

Related

Xcode 4.1 Add and View TabBarController with existing ViewController

First of all sorry for my bad english and Im pretty new in these forums and Xcode programing.
So, Im writing an IPhone app with Xcode 4.1, that has Login and Register stuff visualized with UIViewController. When Im logged in, I need to visualize TabBar with different views.
I tried a lot of stuff and watched a lot of tutorials, all of them just start with the TabBarController, but I don't need it from the beginning, I just need to call it later.
The right way I believe should be just create new file .h, .m and .xib, then add the TabBarController and do a relation between TabBarController - view and File's Owner - view... but it don't let me do this thing. Obviously it don't visualize the right window.
How is the right way to do it?
Please help me, before my hair fall off...
Use the UITabBarController as the root view controller, but display a modal registration / logon view controller over the top when the app begins.
Once the user has logged in, dismiss the modal view controller to reveal the tab bar controller below.
You just use this code in your login button click or next viewcontroller viewwillappers method
UITabBarController *tabbar1 = [[UITabBarController alloc] init];
firstViewcontroller *second = [[firstViewcontroller alloc] initWithNibName:nil bundle:nil];
second.title=#"";
SecondViewController *third=[[SecondViewController alloc]initWithNibName:nil bundle:nil];
third.title=#"";
thirdViewController *one=[[thirdViewController alloc]initWithNibName:nil bundle:nil];
one.title=#"";
tabbar1.viewControllers = [NSArray arrayWithObjects:one, second,third,nil];
tabbar1.view.frame=CGRectMake(0, 0, 320, 460);
[self.view addSubview:tabbar1.view];
I am sure it will work for you i always use this code for create tab bar in any view.

Difficulty displaying Tab Views in iOS iPhone

I'm new to iOS dev and I have a program that begins by presenting the user a view. This view has two buttons, and depending on which the user clicks, a different tab view will be displayed. The tab view is displayed like this:
betaAppDelegate* delegate = (betaAppDelegate*)[[UIApplication sharedApplication]delegate];
acquireData *ac_view = (acquireData*)[[acquireData alloc] init];
[delegate.window addSubview:ac_view.view];
[self.view removeFromSuperview];
[self dealloc];
The tab view is ac_view.view. When I run the application in the simulator, instead of displaying my tab view with three tabs, it displays a white screen with a thin black bar (empty tab dock) on the bottom. It's encouraging to at least see something be displayed! But I've been trying without success for a while to get it to display my tabs. The .xib file looks correct. It has the three tabs at the bottom, and each of the three tabs say in the interface builder that they're loaded from xxxxxxx, so the linking appears correct...
Thank you!
I'm going to presume you're using the UITabBarController.
You can either set one up by adding the tabs in interface builder and then setting which xibs the individual tabs load up. It sounds like you have done this. After that there is no code you need to write to get the tab bar working to switch between your three view controllers.
You can also set up the TabBarController programatically.
This would be the programmatic way and would go into you application delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UITabBarController * aTabBarController = [[UITabBarController alloc] init];
NSArray * array = [[NSArray alloc] initWithObjects:controller1, controller2, controller3, nil];
[aTabBarController setViewControllers:array animated:NO];
[array release];
self.window.rootViewController = aTabBarController;
[self.window makeKeyAndVisible];
[aTabBarController release];
return YES;
}
You would then see a tab bar with three tabs that correspond to controller1, 2 and 3 (your custom view controllers)
To set the icon and text and things its as easy as reading the documentation and seeing
Tab bar items are configured through
their corresponding view controller.
To associate a tab bar item with a
view controller, create a new instance
of the UITabBarItem class, configure
it appropriately for the view
controller, and assign it to the view
controller’s tabBarItem property.
Just a final word of warning in Objective C you should never call dealloc yourself. Dealloc is called by the system when an objects retain count reaches 0. Read into how to retain and release objects to get the hang of how this all works.
Good luck

Switching from one view to second view?

I want to know that can in switch between two views in an iPhone application if I have chosen the application as window based application in the Xcode or it is only possible to switch between views in view based application only.
How to design interface for changing the views in such appliocations as I am not able to design the second view in the interface builder after designing the first view.
Your view controller can present any other view controller like this
[firstViewController presentModalViewController:secondViewController animated:YES];
This will take you to second view controller.
To come back to first view controller, in second view controller you say
[secondViewController.parentViewController dismissModalViewControllerAnimated:YES];
Please refer to the documentation here
The main problem i think you have here is the perception of what each project type does.
A Window based application provides just a window and no "default" view controller for you to use.
A View based application provides a window AND a view controller and xib file for you to create your UI.
If you want to see how to add a view to a window based application create an empty view based application and look at the code that is auto added to the didFinishLaunchingWithOptions method in the appdelegate. This is essentially what you need to do with your window based application.
Add a view controller with a xib file for user interface, then look at how the view based application loads this view and displays it (using initWithNibName and then adding the view to the window)
i'd say you need to do more reading: take a look at cocoa fundamentals for iOS - in the documentation and then the view controller programming guide) these are both essential areas of reading. Then have a root around in the standard project types and take a look at how they are set up, this is really useful because you'll see what apple intend you to do when setting up your app
When you have your class which controls the UIWindow, you add objects in that window. One or some of these objects are Navigation Controllers or Views. in you windowController.h, you should define a view:
#property (nonatomic, retain) IBOutlet UIView *mainView;
and in the .m-file, synthesize it:
#synthesize mainView;
Then use it:
MainView *mainView = [[MainView alloc] initWithNibName:nil bundle:nil];
mainView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mainView animated:YES];
[mainView release];
That is one of the two things that could possibly be your question.
Another question you could ask is: how to switch from my window-based application to a view-based application?
You can just create new classes with corresponding .xib-files, being UIView's. Adapt your appDelegate classes and you should be fine.
Switching from one view to other in view based application
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:firstViewController.view];
or you can also use this.
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:firstViewController animated:YES]; // this is deprecated in ios6.0

TabBarController + TableViewController + Navigation Controller?

I'm new to iphone development, and I want to make an app combining TabBarController + UITableViewController + UINavigationController. I know this question was widely discussed in some video tutorials and threads, but some of them are for old xcode and sdk versions, and I have problems when working through the tutorials.
I will be glad if someone could help me with an up to date tutorial or source files.
To be clear, I'm trying to have my app based on a tab bar controller with two tabs: the second will be a simple view, and the first should contain a tableviewcontroller and a navigation controller. When I click on a cell, it should move to another "detail" view.
Any help will be great.
Ok this is going be long:
in the AppDelegate.h
allocate a UITabBarController a UINavigationController and 2 UIViewControllers
for example:
UITabBarController *mainTabBar;
UINavigationController *navController;
UIViewController *firstViewController;
UIViewController *secondViewController;
then move to AppDelegate.m and instantiate each of those 4 items like this:
mainTabBar = [[UITabBarController alloc] init];
firstViewController = [[firstView alloc] init];
do that for both views
then if you want to set the title of either of the views, (this will be the title that shows up in the tab bar) do it as follows:
firstViewController.title = #"Welcome";
Then create the UINavigationController for the view that has a UITableView inside it like this:
navController = [[UINavigationController alloc] init];
[navController pushViewController:firstViewController animated:NO];
Now you have a UIViewController and a UINavigationController with a UIViewController inside it.
All thats left is to put your two tabs into the UITabBarController:
mainTabBar.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];
then just put the tab bar controller on screen and you should be good to go:
[window addSubview:mainTabBar.view];
A couple things to remember:
make sure you release everything you called alloc on so your using good memory management.
Make sure you import all the files you intend on using in AppDelegate.h, it should look something like: #import "FirstView.h
Let me know in a comment if you have any questions
The document on Apple's developer's library portal, titled Combined View Controller Interfaces, explains how what you are after can be done both through the use of Interface Builder as well as programmatically. I recently followed the documentation on that page to create a part of an application that did entirely what you want to do.

iPhone Sdk: How to add a second UINavigationController?

i created an app from the navigation-based template given by apple. Now i want to add a second navigation controller to my application including a new UITableView. Can anybody show my how to do this? Thanks!
I think this can be done. In your app delegate you normally do something like
[window addSubview:navController.view]. UIWindow is just a UIView. So if you create two UIView ivars in the UIViewController that will contain the two nav controllers you should be able to do a similar thing:
#interface MyViewController : UIViewController
{
UIView* upperView;
UIView* lowerView;
}
etc...
MyUpperRootViewController* myUpperRVC = [[MyUpperRootViewController alloc] init...
UINavigationController* myUpperNavController = [[UINavigationController alloc] initWithRootViewController:myUpperRVC];
[upperView addSubview:navController.view];
[myUpperRVC release];
and something similar on lowerView.
In the root view or subseqeunt views pushed onto the controllers access them in the usual way as if there was one nav controller. [self.navigationController push... should behave normally.
For animating in (and out) the view controllers, just apply the animation to the views - upperView or lowerView. You might want to start with their frames off the visible display and then change them to something visible inside an animation block.