草庐IT

objective-c - shouldAutorotateToInterfaceOrientation 被多次调用 - 这正常吗?

coder 2024-01-27 原文

我的 Split View Controller 代码:

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

这背后的原因可能是什么?我必须提到我没有改变模拟器的方向

最佳答案

shouldAutorotateToInterfaceOrientation 用于检查您的 View 是否支持特定方向。

这并不一定意味着您的设备正在移动/正在旋转。

您不必担心导致外部实体多次查询您的 View Controller 并只为您的 View 返回适当值的实现细节。

如果您对检测设备旋转感兴趣,您可能会决定依赖 UIDeviceOrientationDidChangeNotification

关于objective-c - shouldAutorotateToInterfaceOrientation 被多次调用 - 这正常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7885567/

有关objective-c - shouldAutorotateToInterfaceOrientation 被多次调用 - 这正常吗?的更多相关文章

随机推荐