0

when I lunch my application and I rotate the iPhone in landscape I load a different view (a Help view). Now, if I go to another view through one of the buttons present in the main view and I rotate the iPhone, the Help view shows up...I'd like to know how to do to load the Help View only when I'm in the first View. Here's my .m code:

@implementation Home
@synthesize ruota;
@synthesize portraitView,landscapeView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(orientationChanged:)name:@"UIDeviceOrientationDidChangeNotification" 
object:nil];
[self performSelector:@selector (ritardo) withObject:nil afterDelay:5.0f];
}


-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait) 
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.view = self.portraitView;
    [UIView commitAnimations];
    [self dismissModalViewControllerAnimated:YES];
} else  if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || 
deviceOrientation == UIInterfaceOrientationLandscapeRight) {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.view = self.landscapeView;
    [UIView commitAnimations];
    [self dismissModalViewControllerAnimated:YES];
}
}

-(void) ritardo {
ruota.image = [UIImage imageNamed:@"RuotaPerAiuto.png"];    
}

- (void)viewDidUnload
{
[self setPortraitView:nil]; 
[self setLandscapeView:nil];
[self setRuota:nil];
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;
}
}
@end

EDIT:

-(void)orientationChanged:(NSNotification *)object{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait)
 { 
    if (self.isViewLoaded && self.view.window)
     {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.view = self.portraitView;
    [UIView commitAnimations];
    [self dismissModalViewControllerAnimated:YES];
     }    
} 

else  if (deviceOrientation == UIInterfaceOrientationLandscapeLeft || deviceOrientation 
== UIInterfaceOrientationLandscapeRight) 
{   
    if (self.isViewLoaded && self.view.window)
    {   
     [UIView beginAnimations:@"View Flip" context:nil];
     [UIView setAnimationDuration:0.5f];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
     self.view = self.landscapeView;
     [UIView commitAnimations];
     [self dismissModalViewControllerAnimated:YES];
    }
}
}
Enzoses
  • 115
  • 14

1 Answers1

0

Perhaps you could accomplish this by checking inside your orientationChanged: method whether your first view controller is visible, and then only displaying your modal help screen when that is true:

-(void)orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if (deviceOrientation == UIInterfaceOrientationPortrait) 
    {
        // check if this view controller is visible
        if (self.isViewLoaded && self.view.window) {

            // then show modal help view controller
        }
    }
}

BTW I found the tip of how to check whether the view controller is visible here: How to tell if UIViewController's view is visible

Community
  • 1
  • 1
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • Thankyou for your answer. I edited my code above and now when from the first view I go the another one and I put in landscape there's no Help shown. Good. But when I come back to Home and I put in landscape the last view seen comes up. Strange behavior – Enzoses Mar 14 '12 at 20:52