How can i call the same ViewController from different TabBar items? - iphone

I've developed a ViewController that shows different data according to the input parameter. I would like to use a tabBar interface and call the same ViewController from different tabs by passing them to different parameters.
Can I do this? I get errors if I specify the ViewController's NIB in tabBar item.
Can you help me please?
Thanks in advance.

Create two different instances of your ViewController:
MyViewController *vc1 = [[MyViewController alloc] initWithNib:#"MyViewController" bundle:nil];
MyViewController *vc2 = [[MyViewController alloc] initWithNib:#"MyViewController" bundle:nil];
UITabBarController *tabs = [[UITabBarController alloc] init];
[tabs setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];

Related

Issue with pushing UIViewController on UITabbarController

I have implemented UINavigationController and UITabbarController with each other. I am able to see Navigation bar and Tab bar along with UIViewController.
Problem is when I push any other UIViewController on this controller,that viewcontroller get pushed but Tabbar get disappeared.
Is there any provision to persist that UITabBar along the stack????
below is code I am referring
UIViewController* cont1 = [UIViewController alloc]init];
UIViewController* cont2 = [UIViewController alloc]init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:cont1,cont2,nil]];
Thanks.
You should do it the other way around.
Place an UINavigationController in each of the UIViewController in the UITabbarController.
YourViewController *viewController = [YourViewController alloc] init] autorelease];
UINavigationController *controller = [UINavigationController alloc] initWithRootViewController:viewController] autorelease];
controller.title = #"something";
controller.tabBarItem.image = [UIImage imageNamed:#"xx"];
tabController.viewControllers = [NSArray arrayWithObject:controller];
then you can push sub view controllers into navigation controller with tab bar persistent at the bottom of the screen.

iphone - Tabbar application with NavigationController, without MainWindow.xib

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.. :)

Change view class on a tabbarcontroller

I have one tabbarcontroller set as main controller and its views were configured using interface builder. Now, programmaticaly, I would like to change default class view. How can it be done? For example, one tab view is set from class1 and I would like to set it from class2. Thank you.
I am not sure if this is what you meant, but to assign different view controllers programmatically to a tabbarcontroller you can use this:
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
myTabBarController = [[UITabBarController alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
I would add and remove a tab bar item rather than changing the view controller of a single item.
Probably not animated.

UITabBarController to load subviews

I am kind of stuck and would appreciate any ideas on what I did wrong.
I created programmatically a NSObject which holds a UITabBarController to which three ViewControllers are added:
UITabBarController tabBarController = [[UITabBarController alloc] init];
ControllerOne = [[OneViewController alloc] initWithNibName:#"OneView" bundle:nil];
ControllerTwo = [[TwoViewController alloc]initWithNibName:#"TwoView" bundle:nil];
ControllerThree = [[ThreeViewController alloc] initWithNibName:#"ThreeView" bundle:nil];
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:3];
[viewControllers addObject:ControllerOne];
[viewControllers addObject:ControllerTwo];
[viewControllers addObject:ControllerThree];
[tabBarController setViewControllers:viewControllers];'
I now display the tabBarController's view
viewController.modalTransitionStyle = transitionStyle;
[self presentModalViewController:viewController animated:YES];
with viewController being the just created tabBarController.
The view changes fine, displaying the tabbar correctly (Icons and titles) but fails to show e.g. OneViewController's view. I assume that the view is not loaded since the - (void)viewDidLoad is not being called for any of the subview controllers.
I would appreciate any suggestions.
Thanks,
equi
Are you sure your nib names are how you have typed them? It's not called OneViewController.xib?, for example?
I sorted out the issue.
The above code actually worked, reason for the view not appearing was that I accidentally synthesised 'view' in the UIViewController derivative.

How do I pushViewController/etc. from a UIViewController subclass?

I've been attempting to figure this out for a while now, but I'm up to a point where I can't seem to solve my problem from reading other Q&As. I'm trying to get the active UIViewController in a UINavigationController to send popViewController/pushViewController messages to the UINavigationController, but I cannot figure it out. I'm probably doing something rather stupid that is causing it to break. The structure should be like this, but even then I'm not sure if I've done that right.
mainController
primaryNavigationController
firstViewController
secondViewController
both firstViewController and secondViewController are a subclass
mainController.m
firstViewController = [[FirstTestViewController alloc] init];
secondViewController = [[FirstTestViewController alloc] init];
primaryNavigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
[primaryNavigationController.view setFrame:CGRectMake(0,0,320i,409)];
[self.view addSubview:[primaryNavigationController view]];
[primaryNavigationController.navigationBar setFrame:CGRectMake(0,0,20,44)];
primaryNavigationController.navigationBar.tintColor = [UIColor blackColor];
How can I tell primaryNavigationController to push/pop a VC from within the firstTestViewController subclass?
You would allocate the second view controller within your first view controller (because you don't need it before):
secondViewController = [[FirstTestViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
The SDK includes many sample projects that involve a navigation controller and show you how to do this.