【发布时间】:2023-03-10 09:00:04
【问题描述】:
我正在关注 iTunes 上很棒的斯坦福 cs193p 课程,我现在正在参加第 7 课和第 3 课。
我只是想让 SplitViewController 在 iPad 上正常工作……但看起来我的 UISplitViewControllerDelegate 委托方法在设备方向改变时没有被调用。
这是我目前得到的:
-我创建了一个全新的 iPad Storyboard,添加了一个 SplitViewController,其中一个 UIViewController 作为 Master (CalculatorViewController),另一个 UIViewController 作为 Detail (GraphViewController)。我认为我做的一切都是正确的。
-我的GraphViewController.h实现了UISplitViewControllerDelegate协议:
@interface GraphViewController : UIViewController <UISplitViewControllerDelegate>
-我的 GraphViewController.m 将 SplitViewController 委托设置为自身:
- (void)awakeFromNib {
[super awakeFromNib];
self.splitViewController.delegate = self;
}
-我的GraphViewController.m实现了必要的方法:
// asks the delegate whether the first view controller should be hidden for the specified orientation
- (BOOL)splitViewController:(UISplitViewController *)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation
{
// only show the master controller in landscape mode
return UIInterfaceOrientationIsPortrait(orientation);
}
// tells the delegate that the specified view controller is about to be hidden (must add a popover button)
- (void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
barButtonItem.title = aViewController.title;
// add the button to the toolbar
NSMutableArray *toolbarItems = [self.toolbar.items mutableCopy];
[toolbarItems insertObject:barButtonItem atIndex:0];
self.toolbar.items = toolbarItems;
}
// tells the delegate that the specified view controller is about to be shown again (must remove popover button)
- (void) splitViewController:(UISplitViewController *)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// hide the bar button item on the detail controller
NSMutableArray *toolbarItems = [self.toolbar.items mutableCopy];
[toolbarItems removeObject:barButtonItem];
self.toolbar.items = toolbarItems;
}
-My GraphViewController.m 支持所有方向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES; // support all types of orientation
}
现在,当我运行应用程序(Snow Leopard 上的 Xcode 4.2、iOS 5.0、iPad Simulator 5.0)时,调用了 splitViewController:willHideViewController:withBarButtonItem:forPopoverController: 方法,我的主视图被隐藏,并且我的主视图中显示了一个按钮在 PopOver 中显示主视图的详细视图(因为我处于纵向模式)。
但是,当我更改设备的方向(到横向模式......仍在使用模拟器)时,没有任何反应,上述方法都不会再次被调用。我最终仍然隐藏了主视图并且仍然显示了 PopOver 按钮,而事实上,我希望显示主视图并隐藏 PopOver 按钮。
我做错了什么? 这是我的代码、Xcode 和模拟器的问题吗?
【问题讨论】:
-
如果你使用iOS 6 SDK编译,这个方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation已被弃用,即使你在iOS 5上运行它也不会被调用 -
@MarkE:谢谢,但不,正如我的帖子中所指出的,我在 Snow Leopard、iOS 5.0 和 iPad Simulator 5.0 上使用 Xcode 4.2。我还不想升级,因为我想拥有和 iTunes 课程一样的开发环境
-
@MarkE - 当你说如果你在 iOS 5 上运行它不会被调用,你就错了。这不是它的工作方式。
shouldAutorotateToInterface将始终在 iOS 5 上调用,无论您是否使用 6 SDK 编译二进制文件。