如果我有一个指向UIViewController的指针,是否可以在更改interfaceOrientation时通知我而不修改控制器的代码?
我最好选择检测设备方向的变化,然后看看UIViewController是否会旋转(d)?
答案 0 :(得分: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");
}
答案 1 :(得分:3)
您可以在UIViewController上使用willAnimateRotationToInterfaceOrientation:duration:
方法,然后为横向或纵向重新定位任何UIViews(或任何其他代码)。 E.g。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
// change positions etc of any UIViews for Landscape
} else {
// change position etc for Portait
}
// forward the rotation to any child view controllers if required
[self.rootViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}