Xcode 4.1 Add and View TabBarController with existing ViewController - iphone

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.

Related

Creating a multiview application without storyboards?

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.

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.

Add view to a navigation controller on app launch

I have an app that has a UITabBarController, one of the tabs of which is configured for a navigation controller.
Based on certain logic I need to attach a different root view to the navigation controller inside the tab at application launch time.
This is easily done in interface builder however, because I need to figure out what view to attach at launch time interface builder is not much use to me in this situation.
I'm guessing I will need to perform this in the applicationDidFinishLaunching method in my app delegate class by somehow getting the tab I'm interested in, and pushing the view onto it's navigation controller?
How would I go about this?
Thanks.
You're on the right track. In your app delegate's applicationDidFinishLaunching method, you need to look at whatever your condition is and pick the right thing to set as the UINavigationController's root view controller.
I'm guessing this is a login screen or something? And if you have a cached login from an earlier session you don't show it again? Is that it?
If you take a look at that method in your application delegate, you'll see where the default root view controller is getting instantiated and pushed into the nav controller. Just duplicate that code inside an if() statement. I've done this, it's straightforward.
So what I did in my applicationDidFinishLaunching method was:
// get the array of tabs
NSArray *tabBarArray = tabBarController.viewControllers;
// in my case the navigation controller I'm interested in is in the 4th tab
UINavigationController *navigationController = [tabBarArray objectAtIndex:4];
if(someLogic == true) {
ViewController1 *viewController1 = [[viewController1 alloc] initWithNibName:#"View1" bundle:nil];
[navigationController pushViewController:viewController1 animated:NO];
[viewController1 release];
}
else {
ViewController2 *viewController2 = [[viewController2 alloc] initWithNibName:#"View2" bundle:nil];
[navigationController pushViewController:viewController2 animated:NO];
[viewController2 release];
}
Everything working well.

How do you properly set up a secondary view to support a navigation Controller on the iPhone?

I have an iPhone app that shows a simple view (View 1) that has a button. When the user presses this button, View 2 slides into view using the call
[self presentModalViewController:self.view2 animated:YES];
I want View 2 to support a navigation controller. All the code I find tells you how to set up a Navigation Controller App, but I can't figure out how to set this up using IB.
What I have done is to create a plain view2.xib file. I set the file's owner class to view2.
I add a navigation Controller to the XIB. I create an IBOutlet called view2Nav in view2.h for a UINavigationController. I link view2Nav to the NavigationController in view2.xib.
I then create a view3 class with view3.xib. I set the RootViewController in view2.xib to be of class view3 and set its NIB name to view3.
Then I go back and run the program. When I press my button on view 1, the app crashes as it tries to create view 2.
I know I must be missing a setting or something.
MySecondViewController *secondVC = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondVC];
[self presentModalViewController:navigationController animated:YES];
[secondVC release];
[navigationController release];
Forget about IB. Do anything in code :) It is faster and you will exactly know why and how it works.
I'm not sure whether you can pass a self.view2 to presentModalViewController. If self.view2 is a subclass of UIViewController, you can. If it is a simple UIView, you shouldn't. If fact you can't at all.

UITabBar and more than 1 UINavigationController

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.