没有触发与方向相关的通知。为什么?

时间:2012-08-09 18:00:07

标签: iphone objective-c ios notifications nsnotificationcenter

我希望在设备方向发生变化时收到通知。我已经设置了一个测试方法,可以接收通知。我正在尝试几个不同的观察者来实现这一点,而且没有一个能够正常工作。为什么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");
}

4 个答案:

答案 0 :(得分:1)

我意外地将旋转锁定在我的手机上。总是在多部手机上进行测试!

答案 1 :(得分:0)

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

答案 2 :(得分:0)

bool类型的结束设置方向.write在视图的所有末尾返回yes ..在potrait和横向模式中的方向取消

答案 3 :(得分:0)

如果您只需要方向更改通知,则根本不需要使用beginGeneratingDeviceOrientation。您只需使用didRotateFromInterfaceOrientation:方法。

如果没有被解雇,则意味着您不在正确的视图控制器中。此方法仅在顶视图控制器(即窗口的根视图控制器)中调用 ,但我认为UINavigationControllers会很好并将其交给视图控制器。

换句话说,听起来你正试图在错误的视图控制器中捕获它。