Rotate to fire Segue in iOS 6
Question
I have storyboard which has a tab bar controller. When the device rotates I want to move to a different screen, i.e. not show the same layout sideways but show something completely different.
In iOS 5 I achieved this with the following code in the UITabBarControllerDelegate
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self performSegueWithIdentifier: @"toGraph" sender: self];
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
In iOS 6 this method is no longer called. All the methods I can see deal with when the view is rotated, but not when the device is rotated.
Thanks in advance.
Solution
So really I shouldn't have been looking for a view rotation, but rather a device rotation. After discovering the UIDevice class I was able to use the AlternateViews sample code (just search for AlternateViews in the Documentation Organizer) to get everything I needed.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(showGraphs) withObject:nil afterDelay:0];
}
- (void)showGraphs
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self performSegueWithIdentifier: @"toGraph" sender: self];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
OTHER TIPS
Autorotation has changed in iOS 6. Here's a thread on Apple dev forums on the issue: https://devforums.apple.com/thread/166544?tstart=30
Here are some more threads: http://www.buzztouch.com/forum/thread.php?tid=41ED2FC151397D4AD4A5A60¤tPage=1
https://www.buzztouch.com/forum/thread.php?fid=B35F4D4F5EF6B293A717EB5&tid=B35F4D4F5EF6B293A717EB5
The most relevant post from these to your issue seems to be the following:
Got it working...for tabbed apps, replaced this line in the appDelegate: [self.window addSubview:[self.rootApp.rootTabBarController view]];
with this: [self.window.rootViewController = self.rootApp.rootTabBarController view];
And to get non-tabbed apps, replaced this line: [self.window addSubview:[self.rootApp.rootNavController view]];
with this: [self.window.rootViewController = self.rootApp.rootNavController view];