查看方向更改

时间:2011-07-14 06:35:30

标签: ios objective-c ipad orientation

更新:我发现为什么它一直在加载相同的视图。我现在解决了这个问题这是一个错字

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;
}

这很有效。

现在问题。加载并运行视图时,我更改方向并从同一个nib文件加载不同的视图,修复:但是一旦我这样做,其他UI元素就会消失,只保留我的横向视图的ui元素,当我再次旋转设备它不会再次加载我的纵向视图?有任何建议可能会发生这种情况吗?

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

新问题,每当我更改方向时,它都会加载相反方向的视图

由于

2 个答案:

答案 0 :(得分:1)

我在这里也添加了这个,因为有人曾经读过这个,他们可能想在这里查看这个帖子,其中讨论了一个类似的场景

Two views Multiple UIPickerViews Single outlet

答案 1 :(得分:0)

好的,所以我找到了一个有效的修复程序,但我不知道为什么我的原始代码无法正常工作:

卸下:

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

卸下:

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

变化:

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

        self.view = landscapeView;

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

        self.view = portraintView;

    }
    return YES;
}

现在它完美无缺