以编程方式将 UITabBarController 添加到 UIWindow 后失去旋转支持
我的应用程序一开始只有一个 UIWindow
。我以编程方式将视图控制器添加到 application:didFinishLaunchingWithOptions:
中的 self.window
。
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:
返回YES。旋转对于 myViewController
效果很好,但一旦 tabBarController
可见,旋转就会停止工作,并且界面会以纵向显示。这种行为背后的原因是什么?
另外,在 iOS 4.x 中,UIWindow 具有 rootViewController 属性。这个属性有什么作用?新模板使用 rootViewController
而不是 [self.window addSubview:...]
。这是为什么?
My application starts off with nothing but a UIWindow
. I programmatically add a view controller to self.window
in application:didFinishLaunchingWithOptions:
.
myViewController = [[UIViewController alloc] init:...];
...
[self.window addSubview:myViewController.view];
[self.window makeKeyAndVisible];
At the same time i kick off a background process:
[NSThread detachNewThreadSelector:@selector(startupOperations) toTarget:self withObject:nil];
The startupOperations
look something like this:
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
removes myViewController, creates a UITabBarController
and sets it as the window's main view:
[self.myViewController.view removeFromSuperview];
self.myViewController = nil;
tabBarController = [[UITabBarController alloc] init];
...
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
Questions:
All the view controllers are returning YES for shouldAutorotateToInterfaceOrientation:
. Rotation works fine for myViewController
but as soon as the tabBarController
is made visible, rotation stops working and interface appears in Portrait. What's the reason behind this behavior?
Also, in iOS 4.x, UIWindow has rootViewController property. What's the role of this property? The new templates use rootViewController
instead of [self.window addSubview:...]
. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很奇怪。我尝试在一个简单的基于选项卡栏的项目中模拟您的“视图流”,并且在删除初始控制器并将选项卡栏控制器的视图添加为子视图后,自动旋转有效地工作。
我发现它不起作用的唯一条件是 self.window 确实包含我未删除的第二个子视图。你能在执行的那一刻检查一下
self.window.subview 内容是什么吗?
如果这没有帮助,您能否在问题中分享一下如何初始化
UITabBarController
和UITabBar
?至于你的第二个问题,正如你所说
rootViewController
是属于该窗口的所有视图的根控制器:(源)
您也可以使用它,但请注意已在
applicationDidFinishLaunching
中对其进行分配,否则,如果您“手动”添加子视图并稍后更改此属性,它不会删除您显式添加的子视图。Pretty strange. I tried and simulate your "view flow" in a simple tab bar based project and autorotation effectively works after removing the initial controller and adding the tab bar controller's view as a subview.
The only condition I found where it did not work is when
self.window
did contain a second subview that I did not remove. Could you check at the moment when you executewhat is
self.window.subview
content?If that does not help, could you share in your question how you initialize the
UITabBarController
andUITabBar
?As to your second question, as you say
rootViewController
is the root controller for all the views that belong to the window:(Source)
You can also use that, but take care of assigning it already in
applicationDidFinishLaunching
, otherwise, if you "manually" add a subview and later change this property, it will not remove the subview you explicitly added.