我的拆分视图控制器代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
LeftViewController *hvc = [[[LeftViewController alloc] initWithNibName:nil bundle:nil] autorelease];
DetailViewController *dvc = [[[DetailViewController alloc] initWithNibName:nil bundle:nil] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:hvc] autorelease];
UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:dvc] autorelease];
UISplitViewController *svc= [[[UISplitViewController alloc] init] autorelease];
[svc setViewControllers:[NSArray arrayWithObjects:rootNav, detailNav, nil]];
svc.delegate = dvc;
[window setRootViewController:svc];
[self.window makeKeyAndVisible];
return YES;
}
DetailViewController.m和LeftViewController.m都包含
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSLog(@"should rotate asked to detailviewcontroller");
return YES;
}
在iPad模拟器上,当应用程序刚刚启动时,我可以看到shouldAutorotateToInterfaceOrientation
的这么多来电
should rotate asked to detailviewcontroller
should rotate asked to leftviewcontroller
should rotate asked to leftviewcontroller
should rotate asked to detailviewcontroller
...
should rotate asked to leftviewcontroller // these two lines
should rotate asked to detailviewcontroller // are repeated 13 times
...
should rotate asked to leftviewcontroller
should rotate asked to detailviewcontroller
这背后的原因是什么?我必须提到我不是在改变模拟器的方向
答案 0 :(得分:1)
shouldAutorotateToInterfaceOrientation
用于检查您的视图是否支持特定方向。
这并不一定意味着您的设备正在移动/正在旋转。
您不应该担心导致外部实体多次查询您的视图控制器的实现细节,只需为您的视图返回适当的值。
如果您对检测设备轮换感兴趣,可能会决定依赖UIDeviceOrientationDidChangeNotification
。