present modal view on rotate - iphone

So I have a UITableViewControler displaying a tableview in portrait mode.
As soon as i rotate the iPhone i want to present a modal view in landscape mode.
In the tableView i use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
And to handle the present the modal view:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
{
NSLog(#"Push page view");
PagingViewController *s = [[PagingViewController alloc] initWithNibName:#"PagingView" bundle:nil];
s.hidesBottomBarWhenPushed = YES;
[self presentModalViewController:s animated:YES];
[s release];
}
}
The modal view i have the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
And to dismiss the modal view it self, I do:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
NSLog(#"Dismiss my self");
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
}
Some how this works two times.
The third time i rotate the iPhone from Portrait mode to Landscape mode, i get a bad access error.
I cant figure out what gives me the error.
Anyone care for a shot?

The simplest way I can think of is to implement -shouldAutorotate... and dismiss the modal view and return NO to abort rotation. Perhaps that will be sufficient to avoid any concurrency issues. If this suggestion isn't to your liking take a look at NSNotificationCenter.

Related

iPhone interface orientation change not returning to portrait orientation

So I have a view that I present modally when the interface orientation changes to landscape. However when the orientation returns to portrait and the modal view dismisses itself, the tableview from the initial view remains in landscape orientation (this table view must be only in portrait orientation)
Here is the code :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
[self performSegueWithIdentifier:#"showCatChart" sender:self];
}
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
[self refreshTableView];
}
}
I tried to refresh the tableview but that doesn't make it portrait again ...
this could be from the view hierachy ...
NavigationController->tableView (only portrait)->tableview->landscapeViewModalController
With iOS 6, you need to implement the following:
- (BOOL)shouldAutoRotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
In your AppDelegate, you need to change the following:
[self.window addSubview:viewController.view];
to
[self.window setRootViewController:viewController];
Also, keep your current code if you want to support previous versions of iOS.
Use the following in appdelegate.
[self.window addsubview:viewcontroller];
This alone will solve your orientation problem.

How do I display a landscape coverflow style view when device is rotated?

I'm working on an app that will need to display a coverflow style view in landscape when the device is rotated to landscape orientation. I've worked on a few apps before, but none of them required landscape/rotation so I'm not experienced with it. I have the code to draw the coverflow view, but presenting it is proving tough.
What I'd like is basically like what the iPod app does when displaying coverflow. The view underneath does not rotate, but the coverflow fades in on top, and fades out when rotated back to portrait.
I'm guessing it's something to do with shouldAutorotateToInterfaceOrientation and a modal view being presented with a fade transition, or using the technique found here:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/22285-replicating-cool-ipod-landscape-transition-iphone.html
I guess my main problem is that the modal view is presented, but it's contents are not rotated to landscape. What should I do?
Here's the code I'm using now:
PARENT VIEW
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
NSLog(#"rotating?");
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
NSLog(#"rotating");
CoverFlowViewController *coverFlowView = [[CoverFlowViewController alloc] init];
[coverFlowView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:coverFlowView animated:YES];
[coverFlowView release];
}
return YES;
}
MODAL VIEW
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSLog(#"rotating? going back?");
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
return YES;
} else {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
return NO;
}
You should do the transition inside:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}

Load mainView with shouldAutorotateToInterfaceOrientation

I am trying to load a view with the shouldAutorotateToInterfaceOrientation method, but I have a problem. When the device runs as landscape, I want to show the main view itself, and when when the device is portrait, load a custom UIView.
Here is my code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)|| (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
//doesn't show main view !!
self.view;
else if ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
//works fine !
self.view = portraitView;
return YES;
}
I figured out !!!! just call the view with modal on a method
- (void) mainView {
screenTestViewController *mv = [[screenTestViewController alloc]initWithNibName:#"screenTestViewController" bundle:nil];
[self presentModalViewController:mv animated:NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)|| (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
[self mainView];
.
.
.
You should push your custom UIView in: willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

pushViewController to open a view in landscape

I am trying to open a view in landscape by pushing on navigation controller but it always open in portrait.
First view is in portrait and when I click on a button then next view should be in landscape.
I am trying following code
Calling View:
ResultViewController *resultView = [[ResultViewController alloc] init];
[[self navigationController] pushViewController:resultView animated:YES];
View that should open in landscape:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Is there something else to try here?
Thanks
Try..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//return UIInterfaceOrientationIsLandscape(interfaceOrientation);
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
and then...
- (void)viewDidLoad {
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
}
in resultView

iPad Rotation, Any Way To Load Custom View Based On Orientation?

Im writing an app that I would like to display different views based on orientation. For example, if the device is portrait load pView, if landscape load lView. Below is the code ive currently tried.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
self.view = landscape;
}
}
With this I have created 2 views in IB and connected the outlets to the right view. Ive also tried this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
lView *abo = [[lView alloc] initWithNibName:#"lView" bundle:nil];
[self.navigationController pushViewController:abo animated:NO];
[abo release];
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
lView *abo = [[lView alloc] initWithNibName:#"lView" bundle:nil];
[self.navigationController pushViewController:abo animated:NO];
[abo release];
}
}
The code directly above worked for ipod but not ipad. Any ideas?
I found the following 2 (Apple) sample projects useful:
http://developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html
http://developer.apple.com/library/ios/#samplecode/WhichWayIsUp/Introduction/Intro.html
Also, remember from a device perspective there is also face-up and face-down orientations; this caught me out.
I have to ask the obvious question:
Do you have the screen orientation lock switch turned on for your iPad?
Depending on what you are trying to do, you can set up your interface in IB to handle rotations automatically. You can resize components, move pics and labels around, stuff like that. That may do what you want to do...