iOS(cs193p):为什么我的UISplitViewControllerDelegate委托方法没有调用方向更改?

时间:2013-01-09 16:13:42

标签: ios xcode cs193p

我正在关注iTunes上令人敬畏的斯坦福cs193p课程,我现在正在讲座#7和作业#3。

我只是想让SplitViewController在iPad上正常工作......但是当设备的方向发生变化时,看起来我的UISplitViewControllerDelegate委托方法就不会被调用。

这是我到目前为止所得到的:

- 我创建了一个全新的iPad Storyboard,添加了一个SplitViewController,其中UIViewController作为Master(CalculatorViewController),另一个UIViewController作为Detail(GraphViewController)。我想我做的一切都很正确。

- 我的GraphViewController.h实现了UISplitViewControllerDelegate协议:

@interface GraphViewController : UIViewController <UISplitViewControllerDelegate>

-My 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;
}

- 我的GraphViewController.m支持所有方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES; // support all types of orientation
}

现在,当我运行应用程序(Snow Leopard上的Xcode 4.2,iOS 5.0,iPad模拟器5.0)时,splitViewController:willHideViewController:withBarButtonItem:forPopoverController:方法被调用,我的主视图被隐藏,并且我的按钮显示在我的详细信息视图以在PopOver中显示主视图(因为我处于纵向模式)。

然而,当我改变设备的方向(到横向模式......仍在使用模拟器)时,没有任何反应,上述方法都不会被再次调用。我最终仍然隐藏了主视图并且仍然显示了PopOver按钮,实际上,我希望显示主视图并隐藏PopOver按钮。

我做错了什么? 这是我的代码,Xcode和模拟器的问题吗?

1 个答案:

答案 0 :(得分:1)

解决了问题!

如果它可以帮助别人,这就是我忘了做的事。

在我的CalculatorViewController(SplitViewController Master)中,我不得不允许方向更改为iPad的横向,而不是iPhone:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (self.splitViewController) return YES; 
    else return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

我为GraphViewController(SplitViewController详细信息)做了这个,但忘了为CalculatorViewController做这个......我的错误!