iPhone Dev: Views not fitting in my UINavigation Controller - iphone

I don't use IB much for creating my views, so I usually do everything programatically. I'm bascially pushing a viewController to a navigation controller and it seems the top part of the viewController is under the navigation bar. I'm trying to make the view fit correctly.
Here is my code:
navigationController = [[UINavigationController alloc] initWithNibName:nil
bundle:nil];
[window addSubview:navigationController.view];
viewController = [[UIViewController alloc] init];
//this code was just added to try and make it fit. It doesn't change anything
viewController.view.frame = [UIScreen mainScreen].applicationFrame;
[navigationController pushViewController:viewController animated:NO];

I would be careful using the init method with view controllers - instead look at initWithNibName:bundle:.

Related

view controller does not load with content

I have a button that when clicked shows a view controller. The code for that event is:
and my view controller looks like this:
note the segmented control and background image.
Here is my h and m files of my view controller in case you need them:
when I run my app here is how that view control looks on my iPad:
why do the background image and segmented control do not appear? why are contents not being loaded? It looks like another view controller is being loaded but I have already make sure that I am placing the correct name in the string for the view controller.
- (IBAction) vaClick
{
imgVa.image = [UIImage imageNamed:#"bt-valores.png"];
UIViewController *control = [[NuestrosValoresViewController alloc] initWithNibName:#"NuestrosValoresViewController"
bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc]
initWithRootViewController:control];
[self presentModalViewController:navControl animated:NO];
[navControl setNavigationBarHidden:YES];
[control release];
[navControl release];
//UINavigationController *navControl = [[UINavigationController alloc]
//initWithRootViewController:control];
//[self presentModalViewController:navControl animated:NO];
}
should I release it like this? sorry I basically have to translate a power point presentation to an app therefore I know very little about objective-c. thanks for the help and sorry for the dumb question.
You're not setting the root view controller of your UINavigationController.
Use this line instead when initializing the navigation controller:
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:control];
You'll also want to release navControl after you present it modally. I'll write out the code for you if you post your code instead of using screenshots.

My viewcontroller (in the navcontroller) I load in my app delegate overlaps the tabbar

I'm having trouble initializing a viewcontroller when the app loads in my app delegate. The viewcontroller loads okay, but it overlaps the tabbar that I have at the bottom. Do I need to create another viewcontroller and have it load into that in the app delegate? I currently have a tabBarController set up in my MainWindow.xib, which contains Navigation controllers and inside those are viewControllers.
Here is my code...
In my didFinishLaunchingWithOptions I have:
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:sub];
[window addSubview:nav.view];
Should it be something like?
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewController" bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:sub];
[newViewController.view addSubview:nav.view];
Thanks!
If you're trying to use a UITabBarController, each tab has its own root view controller (these can even be UINavigationController objects). Assuming you have a property for self.tabBarController (I think this gets created by default in Xcode if you are doing a tab-bar app), then:
sub = [[SubGabViewController alloc] initWithNibName:#"SubGabViewContrller" bundle:nil];
nav = [[UINavigationContoller alloc] initWithRootViewController:sub];
[self.tabBarController setViewControllers:[NSArray arrayWithObject:nav] animated:NO];
self.window.rootViewController = self.tabBarController;
// Clean up memory here... only if you don't need references to them
[sub release];
[nav release];
With setViewControllers:animated: you should probably include additional view controllers in the NSArray, otherwise you'll end up with a tab bar only containing one item!
[window addSubview:tabBarController.view]; would be right......
Rather than adding navigation controller using code..... you should add it from Interface Builder.... in interface builder remove the tabBarItem and add a navigationController on it and then set viewController for that navigation controller..........
create an outlet of tabBarController in appDelegate so that you can add it on window. Pleas do not forget to make connection between components in Interface Builder.
Thanks,

UINavigationBar refuses to show in Modal View Controller

I am loading a Modal view controller using the following code in my RootViewController:
[self.navigationController presentModalViewController:accountViewController animated:YES];
In the accountViewController xib file, I have set a navigation bar. My MainWindow.xib and RootViewController.xib also have the navigation bar setup correctly. Additionally, my app delegate has setup the navigation controller (I assume) correctly:
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[window addSubview:navigationController.view];
However, when I load my accountViewController the UINavigationBar is nowhere to be seen. Is it not possible to show a UINavigationBar in a modal view? I was planning to use it to hide the back button, and add a right button...
sha's answer is correct, but I'm giving my own answer to expand on it with a code example to make it clear.
You probably want something like:
- (void)showAccountViewController
{
AccountViewController* accountViewController = [[AccountViewController alloc] initWithNibName:#"AccountView" bundle:nil];
...
// Initialize properties of accountViewController
...
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:accountViewController];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
[accountViewController release];
}
You need to push not viewController but navigationController that has viewController inside.
You can also set the presentation style in the Attribute Inspector to "Current Context". Modal View will not cover the Navigational Bar.

Three20 TTLauncher Issues

So, I'm having some issues with my implementation of the Three20 TTLauncherView. I am using their code, not a fork (although I have heard of rodmaz's version), and I can't get it to work properly. This is what my app looks like.
alt text http://img709.imageshack.us/img709/8792/screenshot20100715at409.png
I removed the icon image, that's not the issue. The issue is, at the top there is no Navigation bar at all, and I believe also causes the white strip at the bottom, which appears to have the same dimensions as a Nav Bar. I've spent quite a while looking through their code and can't figure this out at all. It looks like their Navigation bar (as seen in their Catalog example app) stems from the TTTableViewController, or something further up. However, my app starts like the Facebook app does, not into a table, but into the TTLauncherView. So... how do I get the Navigation bar into my TTLauncher view, if it goes "App Delegate -> TTLauncherView Subclass"
Thanks for your help!
Edit:
Added the code I used. I put this in my app delegate, wrapping my first view with the UINavigation Controller, and it worked just as I wanted!
MainViewController *aController = [[MainViewController alloc] initWithNibName:nil bundle:nil]; //my Main view
self.mainViewController = aController;
[aController release]; //release for Memory Management
self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
UINavigationController *navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:self.mainViewController animated:NO]; //Gets the main view on the screen
[window addSubview:navigationController.view];
You simply wrap the view with a navigation bar before you push the new view. As an example, here is a snippet of my code where I present a modal view controller with a navigation bar.
- (IBAction) showNewNavView: (id) sender
{
// Present it as a modal view and wrap the controller in a navigation controller to provide a navigation bar for the Edit and Save buttons
ModalViewController *addController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
addController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addController release];
}
If you want to add any buttons or set the title of it, you need to do that in the viewDidLoad method of the view that you are pushing (i.e. your TTLauncher view)

Accessing a Top Navigation Controller from a Subview Navigation Controller

I have a my views and controllers set up like so.
A Tab/Bar controller
Within 1. is a root view controller
within 2. is a programmatically created navigation controller, that is displayed as a subview in the root view controller.
What I am trying to do is access the top tab bar/navigation controller so that i can push a view onto it.
I tried parentViewController but all it did was push the view onto the programmed nav controller.
any suggestions?
This is how i set up my root view controller:
-(void)viewDidAppear:(BOOL)animated{
NSLog(#"ROOT APPEARED");
[super viewDidAppear:animated];
WorklistViewController *worklistController = [[WorklistViewController alloc]initWithNibName:#"WorklistView" bundle:[NSBundle mainBundle]];
UINavigationController *worklistNavController = [[UINavigationController alloc] initWithRootViewController:worklistController];
worklistNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
worklistNavController.view.frame = watchlistView.frame;
[worklistNavController.topViewController viewDidLoad];
[worklistNavController.topViewController viewWillAppear:YES];
[self.view addSubview:worklistNavController.view];
GetAlertRequestViewController *alertsController = [[GetAlertRequestViewController alloc]initWithNibName:#"AlertsView" bundle:[NSBundle mainBundle]];
UINavigationController *alertsNavController = [[UINavigationController alloc] initWithRootViewController:alertsController];
alertsNavController.navigationBar.barStyle = UIBarStyleBlackOpaque;
alertsNavController.view.frame = alertsView.frame;
[alertsNavController.topViewController viewDidLoad];
[alertsNavController.topViewController viewWillAppear:YES];
[self.view addSubview:alertsNavController.view];
}
A nested ViewController (ie, inside a view controlled by a ViewController that's actually on the NavController stack) doesn't have direct access to the UINavigationController that its parent's view's controller is a stack member of. That's one MOUTHFUL of a sentence, but the sense of it is: you can't get there from here.
Instead you've got to get at the app's NavController via the App delegate.
YourAppDelegate *del = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:nextViewController animated:YES];
You're using your UIApplication's singleton (contains all sorts of good info about your app), which has a .delegate property pointing to the AppDelegate, and that contains a reference to the NavigationController.
This is how the "Navigation-based Application" Xcode template sets up NavController ownership, anyway. YMMV if you rolled your own--though if you did, you probably wouldn't need to ask this question.
You can use the follow instruccion:
[(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];
It works for me :D
Have a look at UIViewController's navigationController and tabBarController properties. These will return the corresponding navigationController or tabBarController that the given UIViewController 'belongs' to.
So you can do something like:
[customController.navigationController pushViewController:newController animated:YES];
// Similarly for tabBarController ...