我可以观察UIViewController何时更改i​​nterfaceOrientation?

Ben*_*ynn 15 cocoa-touch ios

如果我有一个指向UIViewController的指针,是否可以在更改interfaceOrientation时通知我而不修改控制器的代码?

我最好选择检测设备方向的变化,然后看看UIViewController是否会旋转(d)?

小智 16

您可以使用NSNotificationCenter:

 [[NSNotificationCenter defaultCenter] addObserver:self // put here the view controller which has to be notified
                                         selector:@selector(orientationChanged:)
                                             name:@"UIDeviceOrientationDidChangeNotification" 
                                           object:nil];
- (void)orientationChanged:(NSNotification *)notification{  
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    //do stuff
    NSLog(@"Orientation changed");          
}
Run Code Online (Sandbox Code Playgroud)

  • 嗯.我想我不清楚.我想知道ViewController的明显方向而不是设备.换句话说,我的对象想要观察视图控制器以查看其方向,该方向可能与设备不同,并且在设备改变方向时可能不会改变.这个答案是我在问题第二部分所暗示的内容的一部分. (3认同)