Changing UIView during UIInterfaceOrientation on iPad

I'm trying to change the views of the turn, because my views should differ significantly from portrait to landscape. Now the code that I use works once, after which the application freezes when trying to turn back. Any direction does not matter. For example: if I am in the Landscape and turn to the portrait, everything works fine, until I return to the landscape, it freezes and does nothing.

Here is the code I use to achieve this

In my viewDidLoad method

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];      
[[NSNotificationCenter defaultCenter] addObserver:self  
                                         selector:@selector(didRotate:)  
                                             name:UIDeviceOrientationDidChangeNotification   
                                           object:nil];      

Then I call it for rotation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (void)didRotate:(NSNotification *)notification  
{          
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];  

    if ((orientation == UIDeviceOrientationLandscapeLeft) ||
        (orientation == UIDeviceOrientationLandscapeLeft))
    {  
        // present the other viewController, it only viewable in landscape  
        [self.view addSubview:landScapeView];
    }

    if ((orientation == UIDeviceOrientationLandscapeRight) ||
        (orientation == UIDeviceOrientationLandscapeRight))
    {  
        // present the other viewController, it only viewable in landscape  
        [self.view addSubview:landScapeView];
    }  
    else if ((orientation == UIDeviceOrientationPortrait ||
             (orientation == UIDeviceOrientationPortrait))
    {
        // get rid of the landscape controller  
        [self.view addSubview:portrait];
    }  
    else if ((orientation == UIDeviceOrientationPortraitUpsideDown ||
             (orientation == UIDeviceOrientationPortraitUpsideDown))
    {
        // get rid of the landscape controller  
        [self.view addSubview:portrait];
    }  
}
+1
source share
1 answer

You add your orientation-oriented view when you rotate, but you never delete another, so you need to.

// get rid of the landscape controller  
    ((orientation == UIDeviceOrientationPortraitUpsideDown || orientation == 
UIDeviceOrientationPortraitUpsideDown)) {

[landScapeView removeFromSuperview];      
[self.view addSubview:portrait];
    }  

.

, , , .

+2

All Articles