Hihi all,
I am pretty new in iPhone dev. I have follow some tutorial and created a tabbar application. Below is the code in the appdelegate implementation:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UIViewController *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Then set the title and the image for the tab in each of the controller implementation.
My problem is that, example, in my viewController1, I need to navigate to viewController3, when I use presentModalViewController method to push the viewController3 in, the tabbar at the bottom will be disappeared.
While I tried to use the app delegate to refer to my tabBarController, and use tabBarController.navigationController pushViewController method, my viewController3 is not being pushed, and seems nothing happens.
I have tried to follow a few tutorial, but it's all required to drag in the navigationcontroller into the MainWindow.xib, which, in the xcode 4, MainWindow.xib doesn't exist anymore. How can i create the navigationcontroller from code so that the app can navigate between different view without hiding the tabbar?
Any comment is very much appreciated! Thanks in advance!
:)
If you want to use a navigation controller, you need to create a navigation controller. Since you're not using a XIB, you'll have to create it manually.
Since you want the tab bar to remain visible when you present viewController3, you need to make the navigation controller a child of the tab bar controller.
UIViewController *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
navController1,
viewController2,
nil];
Then when you want to present viewController3, do this:
// in some method of viewController1
[self.navigationController pushViewController:viewController3 animated:YES];
I am not very Sure but have you tried this??? Actually i am going to use XCode 4 soon, i am still using 3.2.8 version:-
WebViewController *viewController = [[WebViewController alloc]initWithNibName:#"WebViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];
See in this also the Tab bar will be removed when you navigate to your 3rd screen, you have to provide the navigation bar to come back.
Hope it helps.. :)
Related
I am working on xcode4.3.2 and I have created tabbed Application 7 now i want to add navigation controller to it, can somebody help me???
In the applicationDidFinishingLoading of your AppDelegate:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *myNav1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *myNav2=[[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:myNav1, myNav2, nil];
self.window.rootViewController = self.tabBarController;
they're lots of tutorial on Internet but here some issue :
Start to create your tabbed application
At every "1st view" of any tabs :
Editor / Embed - in / Navigation Controller
I am new to xcode and trying to understand how UITabBarController works. I have been looking everywhere and could not find a straight solution to this question. In the majority of the examples/tutorials that I see, the UITabBarController is defined in the AppDelegate, and then once you launch the app, you see the tab bar right away. In my app, I want to show a welcome screen first, then once you click "Enter" you get to the tabbar view. So the ideal structure of my objects will be the following:
MyProjectAppDelegate --> MyProjectViewController --> FirstView / SecondView
As far as my understanding, nothing tabbar related should then be declared in MyProjectAppDelegate with this structure. I tried to look at some examples where the UITabBarController is declared in the AppDelegate and do the same in the MyProjectViewController, but nothing happens.
For example, I did this in my MyProjectViewController within an IBAction that is connected to the "Enter" UiButton on my welcome screen:
- (IBAction) EnterApp {
[window addSubview:tabBarController.view];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstView* first = [[FirstView alloc] init];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
SecondView* second = [[SecondView alloc] init];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
NSArray* controllers = [NSArray arrayWithObjects:firstNav,secondNav, nil];
tabBarController.viewControllers = controllers;
[window addSubview:tabBarController.view];
}
Again, this did not do anything once I clicked on the "Enter" button, even though it does the job in the example where I took it from (where it's within the AppDelegate)
I also tried this on my MyProjectViewController, where the tabbar did show up on the First/Second view, but with no option to customize it (just blank black bars with nothing on them and no idea where to configure them):
- (IBAction) EnterApp {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
UIViewController *viewController2 = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
What went wrong here and what should be the right way to go about doing that? A quick example would be highly appreciated.
Thanks!
I have something similar in one of my apps. At first launch it shows a login screen. After the user successfully logs in, the app switches to a tab bar controlled view.
I do the switching in my appdelegate. The login view sends a notification which the app delegate observes and rebuilds the screen:
- (void)switchView:(NSNotification *)notification {
MyTabbarView *homeView = [[MyTabbarView alloc] init];
NSArray *controllers = [NSArray arrayWithObject:homeView];
[mainNavController setViewControllers:controllers animated:YES];
mainNavController.navigationBar.barStyle = UIBarStyleBlack;
mainNavController.navigationBar.hidden = NO;
[homeView release];
}
I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.
The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.
So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.
This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)
This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).
So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.
UITabBarController *tvc = [[UITabBarController alloc] init];
self.tabBarController = tvc;
[tvc release];
// Instantiates three view-controllers which will be attached to the tabbar.
// Each view-controller is attached as rootviewcontroller in a navigationcontroller.
MainScreenViewController *vc1 = [[MainScreenViewController alloc] init];
PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init];
ExerciseViewController *vc3 = [[ExerciseViewController alloc] init];
UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc1 release];
[vc2 release];
[vc3 release];
nvc1.navigationBar.barStyle = UIBarStyleBlack;
nvc2.navigationBar.barStyle = UIBarStyleBlack;
nvc3.navigationBar.barStyle = UIBarStyleBlack;
NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil];
[nvc1 release];
[nvc2 release];
[nvc3 release];
self.tabBarController.viewControllers = controllers;
[controllers release];
This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).
(this is taken from another part of the app)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.detailedAnswerViewController == nil) {
TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init];
self.detailedAnswerViewController = vc;
[vc release];
}
[self.navigationController pushViewController:self.detailedAnswerViewController animated:YES];
}
The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.
I have a problem building applicatin with tabBarController.
There is no problem doing tabBarController with navigationController if I build it from AppDelegate.
But now I have experienced problem when I want to create new view with tabBarController (3 tabs and each has navigation controllers) after a push from previous navigation controller.
It simply doesnt work.
Here is the code:
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:#"MainView_iPhone" bundle:nil];
mainViewController.tabBarItem.title = #"First";
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
DictionariesViewController *dictionariesViewController = [[DictionariesViewController alloc] initWithNibName:#"DictionariesView_iPhone" bundle:nil];
dictionariesViewController.tabBarItem.title = #"Second";
UINavigationController *dictionariesNavigationController = [[UINavigationController alloc] initWithRootViewController:dictionariesViewController];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:mainNavigationController, dictionariesNavigationController, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
There is a problem after view is pushe to "First" controller. Application crashes...
Please for help.
Regards
Borut
What are you trying to do with the following code?
[self.navigationController pushViewController:tabBarController animated:YES];
You said that your app has 3 tabs and each of those tabs have a navigation controller. Therefore, what you should do is to add the navigation controllers to tabBarController.viewControllers (which you did), but then you need to set the tabBarController as the root view controller.
I have done it this way and it works:
registerViewController = [[RegisterViewController alloc] initWithNibName:#"RegisterView_iPhone" bundle:nil];
AppDelegate_Phone *delegatePhone = [[UIApplication sharedApplication] delegate];
[delegatePhone.firstViewController.navigationController pushViewController:registerViewController animated:YES];
Thanks for your help guys.
I am new to iphone development.I have created a view based application.Now i want tab bar in that view.The first view of the tab bar is a table view and the second view is the Web-view.All the tutorials explain only tab bar based application.Since i am using view based application i finding it really hard.How to achieve it by using interface builder.please guide me.Any Sample tutorials will be more useful.Please help me out.Thanks.
Try this one. This code for created a tab bar application in programmatically. So clicked the button it will open the tab bar in the view.
-(IBAction) clkBtn:(id) sender
{
UITabBarController *tabbar1 = [[UITabBarController alloc] init];
firstViewController *first = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: first] autorelease];
secondViewController *second = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: second] autorelease];
tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,nil];
[self.view insertSubview:tabbar1.view belowSubview: first.view];
[self presentModalViewController:tabbar1 animated:YES];
}
Thanks,
Best of Luck.