【问题标题】:No orientation-related notifications are being fired. Why?没有触发与方向相关的通知。为什么?
【发布时间】:2012-08-09 18:00:07
【问题描述】:

我希望在设备方向发生变化时收到通知。我已经设置了一个假设接收通知的测试方法。我正在尝试几个不同的观察者来实现这一点,但他们都没有工作。为什么 testMethod 没有被解雇?

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    // register for orientation change notification

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testMethod)     name:@"UIDeviceOrientationDidChangeNotification" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(testMethod)
                                             name: UIApplicationWillChangeStatusBarOrientationNotification
                                           object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(testMethod)
                                             name: UIApplicationDidChangeStatusBarOrientationNotification
                                           object: nil];
}

- (void)testMethod
{
    NSLog(@"phone was rotated");
}

【问题讨论】:

    标签: iphone objective-c ios notifications nsnotificationcenter


    【解决方案1】:

    我不小心在手机上启用了旋转锁。始终在不止一部手机上进行测试!

    【讨论】:

    • awww 我正准备回答你的问题
    • 我也做了同样的事情!如此容易忽视的事情,它会导致奇怪的行为......
    【解决方案2】:

    您需要使用方向通知,即 UIDeviceOrientationDidChangeNotification。不要将它放在@"UIDeviceOrientationDidChangeNotification" 中,因为您不知道常量的实际内容,即 UIDeviceOrientationDidChangeNotification。看来您正在使用视图控制器。你应该使用 willRotateToInterfaceOrientation:duration: 和 didRotateFromInterfaceOrientation:

    【讨论】:

    • 我正在尝试 -(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration { NSLog(@"rotated"); } 但它仍然没有被解雇
    【解决方案3】:

    bool 类型的结尾设置方向 .write 在视图的所有结束处返回 yes ..在 potrait 和横向模式下发生方向

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2016-04-23
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      相关资源
      最近更新 更多