将rootViewController添加到UINavigationController时出现问题

时间:2009-08-15 04:35:06

标签: iphone uinavigationcontroller

我遇到以下代码的问题:

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];

当我运行我的程序时,我遇到的问题是我的myController视图一直沿着y轴重复,直到屏幕底部。如果我将myController.view添加到根视图,它可以正常工作。当我将myController添加为导航控制器的rootViewController时,我才遇到问题。

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

默认导航控制器项目模板以这种方式定义-applicationDidFinishLaunching:

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

    // Override point for customization after app launch    

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

我意识到你使用alloc init实例化你的导航控制器,而不是从XIB中获取它,但是,你似乎应该将它添加到窗口的视图树中。

您要显示的代码在哪里?

答案 1 :(得分:1)

问题在于我没有指定框架。如果不使用CGRectMake指定框架,则视图控制器只是填充整个空间。

我需要的界限是这样的:

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

答案 2 :(得分:0)

试试这个:

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?

然后在MyViewController的 - (void)viewDidLoad方法中你可以放

self.navigationItem.title = @"List";