Iphone 在除主视图控制器之外的其他控制器中未调用设备方向更改通知事件
在我的应用程序中,我有3个视图控制器,每个视图控制器都通过单击按钮进行导航。在我的第一个视图控制器中,我添加了命令Iphone 在除主视图控制器之外的其他控制器中未调用设备方向更改通知事件,iphone,objective-c,ios,ios6,Iphone,Objective C,Ios,Ios6,在我的应用程序中,我有3个视图控制器,每个视图控制器都通过单击按钮进行导航。在我的第一个视图控制器中,我添加了命令 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Orientchanged:) name:UIDeviceOrientationDidC
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Orientchanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
它仅适用于我的第一个UIViewController
,它是在appdelegate
中添加的。但是它不适用于其他UIViewControllers
并调用选择器
事件。原因是什么?我是否应该向appdelagate添加其他视图控制器
Orientchanged:
方法中的代码示例:
- (void)Orientchanged:(NSNotification *)notification
{
UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
scrollWidth_L = 1024;
scrollWidth_P = 768;
}
您已经为特定的
viewController
添加了一个观察者。您必须添加所有其他viewController
s以了解当前可见viewController
方向已更改
如果您想全局了解,请在appDelegate
中添加观察者
注意:不要忘记在不需要的时候移除观察员
编辑:观察者取决于其包含的位置。在您的示例中,
addObserver:self
,self
是您的第一个视图控制器。您已经为特定的视图控制器添加了一个观察者。您必须添加所有其他viewController
s以了解当前可见viewController
方向已更改
如果您想全局了解,请在appDelegate
中添加观察者
注意:不要忘记在不需要的时候移除观察员
编辑:观察者取决于其包含的位置。在您的示例中,addObserver:self
,self
是您的第一个视图控制器。添加以下方法:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
UIInterfaceOrientation devOrientation = self.interfaceOrientation;
CGFloat scrollWidth_L = self.view.bounds.size.width;
CGFloat scrollWidth_P = self.view.bounds.size.height;
if (UIInterfaceOrientationLandscapeLeft == devOrientation || UIInterfaceOrientationLandscapeRight == devOrientation) {
// Code for landscape setup
} else {
// Code for portrait setup
}
}
添加以下方法:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
UIInterfaceOrientation devOrientation = self.interfaceOrientation;
CGFloat scrollWidth_L = self.view.bounds.size.width;
CGFloat scrollWidth_P = self.view.bounds.size.height;
if (UIInterfaceOrientationLandscapeLeft == devOrientation || UIInterfaceOrientationLandscapeRight == devOrientation) {
// Code for landscape setup
} else {
// Code for portrait setup
}
}
我不明白,你能再解释一下吗。我添加了所有控制器,但它不起作用。也许您应该查看覆盖视图控制器的方向确实更改了方法,而不是侦听全局设备方向更改消息。(请确保覆盖视图控制器子类中的-supportedDeviceOrientations
)无法理解,请再解释一下。我添加了所有控制器,但它不起作用。也许您应该查看覆盖视图控制器的方向确实更改了方法,而不是侦听全局设备方向更改消息。(确保覆盖视图控制器子类中的-supportedDeviceOrientations
)我已将presentviewcontroller用于导航是的,我已在每个控制器的viewDidLoad中添加了addobserver命令。还有其他方法吗?是的,当我单击一个UIViewcontroller中的按钮时,它将导航到另一个UIViewcontroller,并应在加载时检查方向,以获取通知更改事件选择器中声明的一些值。-(void)Orientchanged:(NSNotification*)通知{UIDeviceOrientation devOrientation=[UIDevice currentDevice].orientation;scrollWidth\u L=1024;scrollWidth\u P=768;}类似于我需要为其他控制器执行的代码。我不会跟踪您,但如果您想要标准通知,不需要添加观察员。如果您需要调用自己的通知方法(原因尚未明确),那么您需要为每个控制器调用一个addObserver
。我通常不会使用带有值的硬编码滚动条,而是抓取相应调整大小的self.view.bounds.size.width。也许你应该用(a)所有旋转相关方法的更完整的代码样本更新你的问题;(b)如果你在其他地方使用这个scrollWidth\u L
,也给我们一个片段。为什么你不能在didRotateFromInterfaceOrientation
中执行你的代码,它不需要addObserver
?我使用了presentviewcontroller进行导航是的,我在每个控制器的viewDidLoad中添加了addObserver命令。还有其他方法吗?是的,当我单击一个UIViewcontroller中的按钮时,它将导航到另一个UIViewcontroller,并应在加载时检查方向,以获取通知更改事件选择器中声明的一些值。-(void)Orientchanged:(NSNotification*)通知{UIDeviceOrientation devOrientation=[UIDevice currentDevice].orientation;scrollWidth\u L=1024;scrollWidth\u P=768;}类似于我需要为其他控制器执行的代码。我不会跟踪您,但如果您想要标准通知,不需要添加观察员。如果您需要调用自己的通知方法(原因尚未明确),那么您需要为每个控制器调用一个addObserver
。我通常不会使用带有值的硬编码滚动条,而是抓取相应调整大小的self.view.bounds.size.width。也许你应该用(a)所有旋转相关方法的更完整的代码样本更新你的问题;(b)如果你在其他地方使用这个scrollWidth\u L
,也给我们一个片段。为什么你不能在didRotateFromInterfaceOrientation
中执行你的代码,它不需要addObserver
?这是UI方向,不是设备方向。这是UI方向,不是设备方向。