【问题标题】:View Orientation changes查看方向更改
【发布时间】:2011-07-14 06:35:30
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: ios objective-c ipad orientation


    【解决方案1】:

    我也在此处添加此内容,以防有人读过此内容,他们可能想在此处查看这篇讨论了类似场景的帖子

    Two views Multiple UIPickerViews Single outlet

    【讨论】:

      【解决方案2】:

      好的,所以我找到了一个有效的修复程序,但我不知道为什么我的原始代码不起作用我所做的是:

      删除:

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

      现在完美运行

      【讨论】:

      • 您的示例使用 UIViewController 类。那么,所有 UIViewControllers 的 UIViewControllerRotation 类别呢?有几种旋转方法,例如: - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多