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.
Related
I wanna have a SettingViewMain with a TabBar, which can Flip between SettingView1 and SettingView2.
I tried this simple work since 3 hours and try nearly all tutorials I found, but I don't get it to work.
When I try to add a TabBar programmatically, I can flip between this 2 views, but in this views itself the TabBar is not shown, don't know why. When I add a TabBarController, this is not shown at all.
So, simlpy: How do I add a TabBar on a MasterView (not a AppDelegate-Window or something like this) and get the TabBar to switch between View1 and View2?
You can instantiate a UITabBarController using its alloc and init methods.
Instantiate both other ViewControllers and add them to an array.
After doing so, add it's view to your 'MasterView'.
Code:
UITabBarController *tab = [[UITabBarController alloc] init];
UIViewController *controller1 = [[UIViewController alloc] init];
UIViewController *controller2 = [[UIViewController alloc] init];
NSArray *controllers = [[NSArray alloc] initWithObjects:controller1, controller2, nil];
[tab setViewControllers:controllers];
[[self view] addSubview:[tab view]];
Or something closely compared to this.
Good luck!
Bryan
My UITabBar is not completely showing after I present a UITabBarController from a UIViewController. Please can you tell me what I am doing wrong?
My code is:
//some method
LoggedInViewController *lvc = [[[LoggedInViewController alloc] initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController = [[LoggedInFeedNavigationController alloc] initWithAccount:self.account];
[self.tabController setViewControllers:[NSArray arrayWithObject:navController]];
[self.view addSubview:self.tabController.view];
[super viewDidLoad];
}
It's not a good practice to do:
[viewController1.view addSubview:viewController2.view];
The point of the MVC design is lost. The view controller should get your data (from the model) and put it in the view. If you have more than one view just arrange the functionality of the views to accept the corresponding data.
So if you need a tab bar controller you should do the following:
// assuming you are in the same initial controller
UITabBarController* pTabBarControllerL = [[UITabBarController alloc] init];
MyFirstController* pFirstControllerL = [[MyFirstController alloc] init];
[pTabBarControllerL setViewControllers:[NSArray arrayWithObject:pFirstControllerL]];
// perhaps set more tab bar controller properties - button images and so on
[self presentModalViewController:pTabBarControllerL animated:YES];
// release the memory you do not need
-(void)viewDidLoad {
// do your work in pFirstControllerL
}
PS: You should not subclass UINavigationController and UITabBarController.
Actually according to the Apple's recommendations UITabBarViewController should be the root in the UIWindow hierarchy. We had hard times trying to put TabBar VCs or Navigation VCs not to the root.
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'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.
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];