以编程方式将 UITabBarController 添加到 UIWindow 后失去旋转支持

发布于 2024-11-19 13:53:38 字数 1551 浏览 4 评论 0原文

我的应用程序一开始只有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-11-26 13:53:38

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

我发现它不起作用的唯一条件是 self.window 确实包含我未删除的第二个子视图。你能在执行的那一刻检查一下

 [self.window addSubview:tabBarController.view];

self.window.subview 内容是什么吗?

如果这没有帮助,您能否在问题中分享一下如何初始化 UITabBarControllerUITabBar

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

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

(

您也可以使用它,但请注意已在 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 execute

 [self.window addSubview:tabBarController.view];

what is self.window.subview content?

If that does not help, could you share in your question how you initialize the UITabBarController and UITabBar?

As to your second question, as you say rootViewController is the root controller for all the views that belong to the window:

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文