Reputation: 647
In my application i have 3 view controllers each is navigating by a button click. In my first view controller i have added the command
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Orientchanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
it works only on my first UIViewController
which is added in appdelegate
. But it donot work for other UIViewControllers
and call the selector
event. What is the reason? Should I add other view controllers
to appdelagate.
An example of the code in the Orientchanged:
method:
- (void)Orientchanged:(NSNotification *)notification
{
UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
scrollWidth_L = 1024;
scrollWidth_P = 768;
}
Upvotes: 0
Views: 3674
Reputation: 18157
Add these methods:
- (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
}
}
Upvotes: 3
Reputation: 38249
You have added an observer for the specific viewController
. You will have to add in all other viewController
s for knowing the current visible viewController
orientation changed.
If you want to know globally then add observer in appDelegate
.
Note : Don't forget to remove observer
when not needed.
EDIT : Observer depends on where it contains. Here in your case addObserver:self
, self
is your first view controller.
Upvotes: 1