views:

667

answers:

3

I am having a problem with the following code:

MyViewController *aController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
self.myController = aController;
myController.title = @"List";

[aController release];


UINavigationController *bController = [[UINavigationController alloc] initWithRootViewController:myController];
self.rootNavController = bController;

[bController release];

[self.view addSubview:rootNavController.view];

When I run my program I get the problem where my view for myController is repeated along the y-axis all the way until the bottom of the screen. If I add myController.view to the root view it works ok. I only have the problem when I add myController as the rootViewController of my navigation controller.

Thanks in advance for any help!

A: 

The default navigation controller project template defines -applicationDidFinishLaunching this way:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after app launch    

    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

I realize you're instantiating your nav controller with alloc init rather than getting it from the XIB, however, it seems you ought to be adding it to the window's view tree.

Where is the code you are showing being called from?

Matt Long
I am adding the navigation view to another view controller. I found that its not repeating the view but its stretching it out so that it goes from the navigation bar to the bottom of the screen. Do you think the nav controller is altering the frame dimensions?
Ron Dear
A: 

Try this:

MyViewController *aController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
self.myController = aController;
[aController release];


UINavigationController *bController = [[UINavigationController alloc] initWithRootViewController:myController];
self.rootNavController = bController;
[bController release];

[window addSubview:rootNavController.view];//<--What are you adding the navigationController to??? Another ViewController? TabController? or Window?

Then in the -(void)viewDidLoad method of MyViewController you can put

self.navigationItem.title = @"List";
Jordan
I am adding it to a view controller.
Ron Dear
A: 

The problem was that I did not specify the frame. Without specifying a frame using CGRectMake the view controller was just filling the entire space.

The line I needed was something like this:

rootNavController.view.frame = CGRectMake(0, 0, 320, 431);
Ron Dear