Reputation: 85
I'm following the awesome Stanford cs193p course on iTunes and I'm now at lecture #7 and homework #3.
I'm just trying to make the SplitViewController works correctly on iPad... but it looks like my UISplitViewControllerDelegate delegate methods are not called when the orientation of the device changes.
Here's what I got so far:
-I created a brand new iPad Storyboard, added a SplitViewController with a UIViewController as the Master (CalculatorViewController) and another UIViewController as the Detail (GraphViewController). I think I did everything correctly.
-My GraphViewController.h implements the UISplitViewControllerDelegate protocol:
@interface GraphViewController : UIViewController <UISplitViewControllerDelegate>
-My GraphViewController.m sets the SplitViewController delegate to itself:
- (void)awakeFromNib {
[super awakeFromNib];
self.splitViewController.delegate = self;
}
-My GraphViewController.m implements the necessary methods:
// 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 supports all orientations:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES; // support all types of orientation
}
Now, when I run the application (Xcode 4.2 on Snow Leopard, iOS 5.0, iPad Simulator 5.0), the splitViewController:willHideViewController:withBarButtonItem:forPopoverController: method is called, my Master view is hidden and a button is shown in my Detail view to display the Master view in a PopOver (cause I'm in portrait mode).
However, when I change the orientation of the device (to landscape mode... still using the simulator), nothing happens, none of the above methods get called again. I end up with the Master view still hidden and the PopOver button still displayed, when in fact, I would like the Master view to be displayed and the PopOver button to be hidden.
What am I doing wrong? Is this a problem with my code, with Xcode, with the simulator?
Upvotes: 1
Views: 1560
Reputation: 85
Problem Solved!
In case it might help others, here's what I forgot to do.
In my CalculatorViewController (the SplitViewController Master), I had to allow orientation changes to landscape for the iPad, but not for the iPhone:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (self.splitViewController) return YES;
else return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
I did it for the GraphViewController (the SplitViewController Detail) but forgot to do it for the CalculatorViewController... my mistake!
Upvotes: 1