【发布时间】:2011-10-25 13:55:48
【问题描述】:
背景和目标:我有一个基于 UISplitViewController 的 iPad 应用程序 - 到目前为止它支持 4 个方向,但现在我想将其锁定为仅横向。
我将左视图控制器的shouldAutorotateToInterfaceOrientation 更改为仅支持横向模式,但这会阻止其viewWillAppear 被调用。
详情:我的 iPad 的视图控制器组织如下:
window
`-- splitVC (UISplitViewController)
`-- rootNav (UINavigationController)
`-- hvc (HostManagerViewController, derived from UIViewController)
`-- detailViewController (DetailViewController, derived from UIViewController)
这在 App Delegate 中实现如下:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
HostManagerViewController *hvc = [[[HostManagerViewController alloc]
initWithNibName:nil bundle:nil] autorelease];
self.detailViewController = [[[DetailViewController alloc]
initWithNibName:nil bundle:nil] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc]
initWithRootViewController:hvc] autorelease];
UISplitViewController *splitVC= [[[UISplitViewController alloc] init] autorelease];
[splitVC setViewControllers:[NSArray arrayWithObjects:rootNav,
detailViewController, nil]];
splitVC.delegate = detailViewController;
[window addSubview:splitVC.view];
[window setRootViewController:splitVC];
return YES;
}
DetailViewController.m 和 HostManagerViewController.m 都包含时调用viewWillAppear
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Console output:
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Hostmanager: Viewdidload
Should rotate called to hostmanager with 1
Hostmanager: viewwillappear
但是当我把HostManagerViewController的代码改成
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
然后不调用 HostManagerViewController 的“viewWillAppear”。控制台输出
Should rotate called to hostmanager with 1 (1 is the numeric value of interfaceOrientation)
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 1
Should rotate called to hostmanager with 3
Hostmanager: Viewdidload
Should rotate called to hostmanager with 1
Info.plist 中仅支持横向模式
编辑:插入 NSLog 消息以跟踪 shouldAutorotateToInterfaceOrientation、viewWillAppear 和 ViewDidLoad
【问题讨论】:
-
您可以上传这个项目的样本吗?
-
你在哪个 iOS 版本上运行这个?
-
我使用的是 4.3。可以将其缩减为更小的测试用例。那我在哪里上传呢?
-
您尝试过我帖子中的建议吗?很有可能会解决您的问题。您正在将 SplitViewController 委托设置为 detailviewcontroller,如果这将为更改方向返回 YES,那么它将尝试旋转位于 SplitViewController 内的所有控制器,然后您的 HostManagerViewController 将拒绝更改方向(这可能是您的问题) .
标签: objective-c ios ipad