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.
Related
I'm a beginner leearning Xcode, and I created a simple single view application for a pretty useless card game (it's not even really a game).
What I want to know is is it possible to import my single view application into an application with storyboards without having to make a new project and retyping all of the code and connections?
Or is it possible to make a multiview application without storyboards, that I could continue off in the same project?
If so, can anyone direct me to a resource to do so? Thanks.
You can make a multiview application without using Storyboard.
Make a new UIViewController with a nib
Apple's example of presented a view controller programmatically
- (void)add:(id)sender {
// Create the root view controller for the navigation controller
// The new view controller configures a Cancel and Done button for the
// navigation bar.
RecipeAddViewController *addController = [[RecipeAddViewController alloc]
init];
// Configure the RecipeAddViewController. In this case, it reports any
// changes to a custom delegate object.
addController.delegate = self;
// Create the navigation controller and present it.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];
}
If you are using an nib though you would want to allocated as followed:
RecipeAddViewController *addController = [[RecipeAddViewController alloc]
initWithNibName:#"yourNibName" bundle: nil];
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW1
You can easily just copy your source and header files of choice to whichever project you like (beware of enabled/disabled ARC, though). Also, you can mix storyboards and nib files as you wish. Here's a little documentation on nibs that you might find interesting. Basically, you can init a UIViewController with it's nib file like this:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
Yes, you can go with #Alex answer.
And you can also add "Storyboard" in your project.
Steps to add storyboard are as follows:-
1. Add new file => Click on "User Interface" tab on left side => Select storyboard => Choose Device type => and create. A blank storyboard file has been created.
2. Now, drag-drop a UIViewController from list of objects(right side) and reference it with your view controller class.
In the same way, you can add other views using storyboard and create multi-view app.
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.
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").
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
I'm currently working on an app that require that I have different UINavigationControllers - So I'm using a tab bar and attempting to use a UITabBar to swap between them so I have in the app delegate a bit of code like so:
// Setting up the views for the tab controller
Friends *friends = [[[Friends alloc] initWithNibName:#"Friends" bundle:[NSBundle mainBundle]] autorelease];
WifiManager *wifi = [[[WifiManager alloc] initWithNibName:#"WifiManager" bundle:[NSBundle mainBundle]] autorelease];
UINavigationController *locationController = [[UINavigationController alloc] initWithRootViewController:wifi];
UINavigationController *friendsController = [[UINavigationController alloc] initWithRootViewController:friends];
//Set up the tab controller
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers =
[NSArray arrayWithObjects:locationController, friendsController, nil];
//Add the tab bar to the window
[window addSubview:tabBarController.view];
This will compile and will load up the first UINavigationController but when I click on the other navigation controller I get:
*** -[NSCFData tabBarItem]: unrecognized selector sent to instance 0x1152b0'
The strangest part is I can use the tab controller with a single UINavigationController and everything works as it should but when I try and add the second one it fails miserably - has anyone got any ideas as to what I'm doing so wrong here?
Thank you in advance
James
Have you verified that each of the single view controllers (Friends and WifiManager) work when there is only one? It could be that your problem is not "two controllers", but "one controller that is broken."
That code should work.
The only thing I can think to suggest, is to not autorelease the view controllers you are creating.
I have an application that works in a similar way. The way I handle this is to abstract it a little more. That is to say, let the top level (below the default window and stuff) be only the tab-bar controller. I would suggest deriving a custom class here so that you can get at the code. Then have each nav-bar controller reside within that tab-bar (within the nib-structure), but only worry about adding them when they are displayed.
Using Interface Builder: It was pretty simple to do with IB. In my MainWindow.xib file, the top level has all the normal stuff, Window, the generic UITabBarController and the UIViewControllers that I wish to push onto each UINavigationController, we'll say UIViewController 1b and 2b (1a and 2a are the two UIViewControllers that are the default views for each respective navbar). Nested within the UITabBarController, I have the UITabBar and my two UINavigationControllers. Within each, I have a UINavigationBar, a UIViewController, and a UITabBarItem, in that order. Here's what my code looks like in the app delegate:
[window addSubview:tabBarController.view];
[tabBarController setSelectedIndex:0];
Then when I want to make use of the navbars I do this:
UIViewController* newView = [[UIViewController alloc]initWithNibName:#"newViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:TRUE];
[newView release];
That's all it takes for me to get it to work (I might have forgotten some IB wiring).
My final thought is that the bundle might be messing with your navbars. I have never used it and don't know much about the pros and cons, but you might try killing that to see if it's a fix, at least temporarily.