0

我的应用程序开始时只有一个UIWindow. 我以编程方式将视图控制器添加到self.windowin application:didFinishLaunchingWithOptions:

myViewController = [[UIViewController alloc] init:...];
...

[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];

同时我启动了一个后台进程:

[NSThread detachNewThreadSelector:@selector(startupOperations) toTarget:self withObject:nil];

startupOperations看起来像这样:

NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];

// Load data
...

// When your done, call method on the main thread
[self performSelectorOnMainThread:@selector(showMainViewController) withObject:nil waitUntilDone:false];

// Release autorelease pool
[threadPool release];

showMainViewController删除 myViewController,创建一个UITabBarController并将其设置为窗口的主视图:

[self.myViewController.view removeFromSuperview];
self.myViewController = nil;

tabBarController = [[UITabBarController alloc] init];
...

[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

问题:

所有视图控制器都为shouldAutorotateToInterfaceOrientation:. 旋转工作正常,myViewController但一旦tabBarController可见,旋转停止工作并且界面出现在肖像中。这种行为背后的原因是什么?

此外,在 iOS 4.x 中,UIWindow 具有 rootViewController 属性。这个属性的作用是什么?新模板使用rootViewController而不是[self.window addSubview:...]. 这是为什么?

4

1 回答 1

1

很奇怪。我尝试在一个基于标签栏的简单项目中模拟您的“视图流”,并且在删除初始控制器并将标签栏控制器的视图添加为子视图后,自动旋转有效地工作。

我发现它不起作用的唯一条件是何时self.window包含我没有删除的第二个子视图。您可以在执行时检查吗

 [self.window addSubview:tabBarController.view];

什么是self.window.subview内容?

如果这没有帮助,您能否分享您如何初始化UITabBarControllerand的问题UITabBar

至于你的第二个问题,正如你所说rootViewController的是属于窗口的所有视图的根控制器:

根视图控制器提供窗口的内容视图。将视图控制器分配给此属性(以编程方式或使用 Interface Builder)将视图控制器的视图安装为窗口的内容视图。如果窗口具有现有的视图层次结构,则在安装新视图之前删除旧视图。

来源

您也可以使用它,但注意已经在 中分配它applicationDidFinishLaunching,否则,如果您“手动”添加子视图并稍后更改此属性,它不会删除您显式添加的子视图。

于 2011-07-11T13:35:04.817 回答