【问题标题】:UIDevice currentDevice's "orientation" always nullUIDevice currentDevice 的“方向”始终为空
【发布时间】:2011-01-28 12:18:29
【问题描述】:

根据标题。 调用 [[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications] 无效。

DidRotateToInterfaceOrientation 等事件工作正常,但我需要能够任意轮询设备方向。

我该如何解决/做到这一点?

长篇大论: 我有一个标签应用程序,每个标签上都有一个导航控制器。第一个选项卡的根视图是当方向变为横向时全屏显示的图形;但是,每当视图出现时都需要检查这一点,因为方向变化可能已经发生在其他地方,所以我希望在视图出现时轮询方向状态。

【问题讨论】:

    标签: iphone objective-c orientation uidevice interface-orientation


    【解决方案1】:

    UIDevice 的方向概念似乎只在实际设备上可用。无论通知是否已按照文档的建议启用,模拟器似乎总是在这里返回 0。令人讨厌的不方便,但你去吧。

    我发现这在实际设备上运行良好:

        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]);
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    

    【讨论】:

    • 是的,只是浪费了 2 小时试图找出问题所在,但实际上它只是模拟器。在设备上一切正常。
    • [[UIDevice currentDevice]orientation] 如果用户已从 Springboard 锁定了他的设备方向,则不起作用。方向始终是最后一个值,并且没有更改通知。
    • UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;这个替代方案对我有用
    【解决方案2】:

    似乎是一个愚蠢的问题,但不是吗

    beginGeneratingDeviceOrientationNotifications

    ( 小写 b ) ...

    【讨论】:

      【解决方案3】:

      如果您在- (void)viewDidLoad 中检查[[UIDevice currentDevice] orientation],您将永远得到零。

      在*- (void) viewDidAppear:(BOOL)*动画方法中检查它和你

      【讨论】:

      • "...你总会得到nil"。这有记录吗?
      【解决方案4】:

      这正如 iMeMyself 在上面的 cmets 中所说的那样——这让我花了很多时间,我认为这是正确的答案,所以我想在这里强调一下:

      UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
      
      if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
      {
         //do stuff here
      }
      else if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
      {
      //or do stuff here
      }
      

      【讨论】:

      • 这对我有用,但不应该是UIInterfaceOrientation interfaceOrientation=[.... 吗?
      【解决方案5】:

      [[UIDevice currentDevice]orientation] 不会给你当前的方向状态吗?您可以在要轮询的视图控制器的 viewWillAppear 方法中检查这一点。

      编辑:除此之外,还有多种方法可以获取当前方向,例如使用 UIApplication 中的 statusBarOrientation 属性,或 UIViewcontroller 中的 interfaceOrientation 属性。

      【讨论】:

      • 看来您没有阅读该问题。他说该方法返回 NULL,即使他正在调用 beginGeneratingOrientationNotifications。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多