简体   繁体   中英

View Orientation changes

Update: I found why it was loading the same view the whole time. I now fixed that. It was a typo

ControllerForSplitViews.m

- (id)init
{
    self = [super initWithNibName:@"ControllerForSplitViews" bundle:nil];
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
        self.view = landscapeView;
    }
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
        self.view = portraintView;
    }
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    return self;
}

This works.

Now the problem. When the view is loaded and running I change the orientation and load a different view from the same nib file, Fixed: but as soon as I do this the other UI elements disappears and only the ui elements of my landscape view is kept and when I rotate the device again it doesn't load my portrait view again? Any suggestions why this might happen?

-(void) orientationChanged:(id)object
{   
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        self.view = portraintView;
    }
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        self.view = landscapeView;
    }
 }

New Problem, each time I change the orientation it loads the opposite orientation's view

Thanks

I am adding this here as well for incase someone ever reads this, they might want to check out this post here where a simular scenario was discussed

Two views Multiple UIPickerViews Single outlet

Ok so I found a fix that works, but I have no idea why my original code didn't work what I did was:

Remove:

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

Remove:

-(void) orientationChanged:(id)object
{   
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        self.view = portraintView;
    }
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        self.view = landscapeView;
    }
 }

Change:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){

        self.view = landscapeView;

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
          (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){

        self.view = portraintView;

    }
    return YES;
}

And now it works perfectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM