【发布时间】:2009-09-27 23:38:00
【问题描述】:
我设置了三个 ViewController 来处理三个视图。我遇到的问题是,在模拟器中,方向是 LandscapeRight (这就是我想要的),并且第一个视图在该景观视图中正确显示,但是当我移动到第二个和第三个视图时,它们会出现逆时针旋转 90 度,视图的左上角位于手机屏幕的左下角。几天来我一直在尝试调试它,我得到的最接近线索的方法是通过以下方式对其进行跟踪:
以下是我的应用委托的 applicationDidFinishLaunching:
NSLog(@"1");
[window addSubview:welcomeController.view];
NSLog(@"2");
[window addSubview:goalController.view];
NSLog(@"3");
[window addSubview:planningController.view];
NSLog(@"4");
[window bringSubviewToFront:welcomeController.view];
NSLog(@"5");
我的每个 ViewController 都实现了类似于以下内容(唯一的变化是控制器的名称在传递给 NSLog 的字符串中切换出来):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
NSLog(@"called for WelcomeController");
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
这样,我在控制台上得到以下输出:
a
called for WelcomeController
called for WelcomeController
called for WelcomeController
called for WelcomeController
2
called for GoalController
3
called for PlanningController
4
5
我觉得有趣的是 shouldAutorotateToInterfaceOrientation 在添加的第一个视图中被调用了 4 次,而另外两个只被调用了一次。我预计这可能是因为它首先必须进行一些设置(并且我相信模拟器以纵向模式启动,所以它可能会在旋转时调用它),但我发现相关性有点可疑。
我改变了顺序,首先为goalController 调用addSubview,然后为welcomeController 调用。在这种情况下,goalController 以正确的横向显示(通常是欢迎控制器)。这似乎消除了我的 XIB 文件和 ViewControllers 本身。我不确定为什么调用 addSubview 的第一个视图很特殊。我还尝试在索引 0 处使用 insertSubview,结果相同。
【问题讨论】:
标签: iphone cocoa-touch